7.2 Newton’s Method
187
the solution and the number of function calls. The main cost of a method for solving
f (x) = 0 equations is usually the evaluation of f (x) and f (x), 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 f (x) and then one call to f and
one to f in each iteration.
Running Newtons_method.py, we get the following printout on the screen:
Number of function calls: 25
A solution is: 3.000000
The Newton scheme will work better if the starting value is close to the solution.
A good starting value may often make the difference as to whether the code actually
finds a solution or not. Because of its speed (and when speed matters), Newton’s
method is often the method of first choice for solving nonlinear algebraic equations,
even if the scheme is not guaranteed to work. In cases where the initial guess may
be far from the solution, a good strategy is to run a few iterations with the bisection
method (see Sect. 7.4) to narrow down the region where f is close to zero and then
switch to Newton’s method for fast convergence to the solution.
Using sympy to Find the Derivative Newton’s method requires the analytical
expression for the derivative f (x). Derivation of f (x) is not always a reliable
process by hand if f (x) is a complicated function. However, Python has the
symbolic package SymPy, which we may use to create the required dfdx function.
With our sample problem, we get:
import sympy as sym
x = sym.symbols(’x’)
f_expr = x**2 - 9
# symbolic expression for f(x)
dfdx_expr = sym.diff(f_expr, x) # compute f’(x) symbolically
# turn f_expr and dfdx_expr into plain Python functions
f = sym.lambdify([x],
# argument to f
f_expr)
# symbolic expression to be evaluated
dfdx = sym.lambdify([x], dfdx_expr)
print(f(3), dfdx(3))
# will print 0 and 6
The nice feature of this code snippet is that dfdx_expr is the exact analytical
expression for the derivative, 2*x (seen if you print it out). This is a symbolic
expression, so we cannot do numerical computing with it. However, with lambdify,
such symbolic expression are turned into callable Python functions, as seen here
with f and dfdx.
The next method is the secant method, which is usually slower than Newton’s
method, but it does not require an expression for f (x), and it has only one function
call per iteration.
187
the solution and the number of function calls. The main cost of a method for solving
f (x) = 0 equations is usually the evaluation of f (x) and f (x), 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 f (x) and then one call to f and
one to f in each iteration.
Running Newtons_method.py, we get the following printout on the screen:
Number of function calls: 25
A solution is: 3.000000
The Newton scheme will work better if the starting value is close to the solution.
A good starting value may often make the difference as to whether the code actually
finds a solution or not. Because of its speed (and when speed matters), Newton’s
method is often the method of first choice for solving nonlinear algebraic equations,
even if the scheme is not guaranteed to work. In cases where the initial guess may
be far from the solution, a good strategy is to run a few iterations with the bisection
method (see Sect. 7.4) to narrow down the region where f is close to zero and then
switch to Newton’s method for fast convergence to the solution.
Using sympy to Find the Derivative Newton’s method requires the analytical
expression for the derivative f (x). Derivation of f (x) is not always a reliable
process by hand if f (x) is a complicated function. However, Python has the
symbolic package SymPy, which we may use to create the required dfdx function.
With our sample problem, we get:
import sympy as sym
x = sym.symbols(’x’)
f_expr = x**2 - 9
# symbolic expression for f(x)
dfdx_expr = sym.diff(f_expr, x) # compute f’(x) symbolically
# turn f_expr and dfdx_expr into plain Python functions
f = sym.lambdify([x],
# argument to f
f_expr)
# symbolic expression to be evaluated
dfdx = sym.lambdify([x], dfdx_expr)
print(f(3), dfdx(3))
# will print 0 and 6
The nice feature of this code snippet is that dfdx_expr is the exact analytical
expression for the derivative, 2*x (seen if you print it out). This is a symbolic
expression, so we cannot do numerical computing with it. However, with lambdify,
such symbolic expression are turned into callable Python functions, as seen here
with f and dfdx.
The next method is the secant method, which is usually slower than Newton’s
method, but it does not require an expression for f (x), and it has only one function
call per iteration.
