Page - 187 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 187 -
Text of the Page - 187 -
7.2 Newton’sMethod 187
thesolutionandthenumberof functioncalls.Themaincostofamethodforsolving
f(x)=0equationsisusually theevaluationoff(x)andf ′(x), so the totalnumber
ofcalls to thesefunctionsisan interestingmeasureof thecomputationalwork.Note
that in functionNewton there is an initial call tof(x) and then one call tof and
one tof ′ ineach iteration.
RunningNewtons_method.py,we get the followingprintouton thescreen:
Number of function calls: 25
A solution is: 3.000000
TheNewtonschemewillworkbetter if thestartingvalue isclose to thesolution.
Agoodstartingvaluemayoftenmakethedifferenceas towhether thecodeactually
finds a solution or not. Because of its speed (and when speed matters), Newton’s
methodisoftenthemethodoffirstchoiceforsolvingnonlinearalgebraicequations,
even if the scheme is not guaranteed to work. In cases where the initial guess may
be far fromthesolution,agoodstrategy is to runa fewiterationswith the bisection
method(see Sect.7.4) to narrowdownthe regionwheref is close to zeroand then
switch toNewton’smethodfor fast convergenceto thesolution.
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
symbolicpackageSymPy, which we may use to create the requireddfdx function.
Withoursampleproblem,weget:
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,sowecannotdonumericalcomputingwithit.However,withlambdify,
such symbolic expression are turned into callable Python functions, as seen here
withfanddfdx.
The next method is the secant method, which is usually slower than Newton’s
method,but itdoesnot requireanexpressionforf ′(x), and ithasonlyonefunction
callper iteration.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik