solving nonlinear algebraic equations solution and the number of function calls the main cost of method for solving xd equations is usually the evaluation of xand /so the total number of calls to these functions is an interesting measure of the computational work note that in function newton there is an initial call to xand then one call to and one to in each iteration running newtons_method pywe get the following printout on the screennumber of function calls solution is as we did with the integration methods in we will collect our solvers for nonlinear algebraic equations in separate file named nonlinear_solvers py for easy import and use the first function placed in this file is then newton the newton scheme will work better if the starting value is close to the solution good starting value may often make the difference as to whether the code actually finds solution or not because of its speednewton' method is often the method of first choice for solving nonlinear algebraic equationseven if the scheme is not guaranteed to work in cases where the initial guess may be far from the solutiona good strategy is to run few iterations with the bisection method (see to narrow down the region where is close to zero and then switch to newton' method for fast convergence to the solution newton' method requires the analytical expression for the derivative xderivation of xis not always reliable process by hand if xis complicated function howeverpython has the symbolic package sympywhich we may use to create the required dfdx function in our sample problemthe recipe goes as followsfrom sympy import symbols(' 'define as mathematical symbol f_expr ** symbolic expression for (xdfdx_expr diff(f_exprxcompute '(xsymbolically turn f_expr and dfdx_expr into plain python functions lambdify([ ]argument to f_exprsymbolic expression to be evaluated dfdx lambdify([ ]dfdx_exprprint dfdx( will print the nice feature of this code snippet is that dfdx_expr is the exact analytical expression for the derivative *xif you print it out this is symbolic expression so we cannot do numerical computing with itbut the lambdify constructions turn symbolic expressions into callable python functions the next method is the secant methodwhich is usually slower than newton' methodbut it does not require an expression for /and it has only one function call per iteration