solving nonlinear algebraic equations linspace( (xroot none initialization for in range(len( )- )if [ ]* [ + root [ ( [ + [ ])/( [ + [ ])* [ibreak jump out of loop if root is noneprint 'could not find any root in [% % ]( [ ] [- ]elseprint 'find (the firstroot as =%groot (see the file brute_force_root_finder_flat py note the nice use of setting root to nonewe can simply test if root is none to see if we found root and overwrote the none valueor if we did not find any root among the tested points running this program with some functionsay xd cos (which has solution at )gives the root which has an error of : increasing the number of points with factor of ten gives root with an error of : after such quick "flatimplementation of an algorithmwe should always try to offer the algorithm as python functionapplicable to as wide problem domain as possible the function should take and an associated interval oeabas inputas well as number of points ( )and return list of all the roots in oeabhere is our candidate for good implementation of the brute force rooting finding algorithmdef brute_force_root_finder(fabn)from numpy import linspace linspace(abny (xroots [for in range( - )if [ ]* [ + root [ ( [ + [ ])/( [ + [ ])* [iroots append(rootreturn roots (see the file brute_force_root_finder_function py this time we use another elegant technique to indicate if roots were found or notroots is an empty list if the root finding was unsuccessfulotherwise it contains all the roots application of the function to the previous example can be coded as def demo()from numpy import expcos roots brute_force_root_finderlambda xexp(- ** )*cos( * ) if rootsprint roots elseprint 'could not find any roots