Seite - 196 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 196 -
Text der Seite - 196 -
196 6 SolvingNonlinearAlgebraicEquations
solution and the number of function calls. Themain cost of amethod for solving
f.x/D 0 equations isusually the evaluationoff.x/ andf 0.x/, so the total num-
berof calls to these functions is an interestingmeasureof thecomputationalwork.
Note that in functionNewton there is an initial call tof.x/ and thenonecall tof
andone tof 0 in each iteration.
RunningNewtons_method.py,weget the followingprintouton the screen:
Number of function calls: 25
A solution is: 3.000000
Aswedidwith the integrationmethods inChapter3,wewill collectour solvers
fornonlinearalgebraicequationsinaseparatefilenamednonlinear_solvers.py
foreasy importanduse. Thefirst functionplaced in thisfile is thenNewton.
TheNewtonschemewillworkbetter if thestartingvalue isclose to thesolution.
Agoodstartingvaluemayoftenmakethedifferenceas towhether thecodeactually
findsa solutionornot. Becauseof its speed,Newton’smethod is often themethod
offirst choice for solving nonlinear algebraic equations, even if the scheme is not
guaranteed towork. In caseswhere the initial guessmaybe far from the solution,
agoodstrategyis torunafewiterationswiththebisectionmethod(seeChapter6.4)
to narrowdown the regionwheref is close to zero and then switch toNewton’s
methodfor fast convergenceto thesolution.
Newton’s method requires the analytical expression for the derivative f 0.x/.
Derivation of f 0.x/ is not always a reliable process by hand if f.x/ is a com-
plicated function. However, Python has the symbolic package SymPy, whichwe
may use to create the required dfdx function. In our sample problem, the recipe
goesas follows:
from sympy import *
x = symbols(’x’) # define x as a mathematical symbol
f_expr = x**2 - 9 # symbolic expression for f(x)
dfdx_expr = diff(f_expr, x) # compute f’(x) symbolically
# Turn f_expr and dfdx_expr into plain Python functions
f = lambdify([x], # argument to f
f_expr) # symbolic expression to be evaluated
dfdx = lambdify([x], dfdx_expr)
print dfdx(5) # will print 10
The nice feature of this code snippet is thatdfdx_expr is the exact analytical
expression for thederivative,2*x, if youprint it out. This is a symbolic expression
sowecannotdonumericalcomputingwith it, but thelambdifyconstructions turn
symbolicexpressions intocallablePythonfunctions.
The nextmethod is the secantmethod,which is usually slower thanNewton’s
method,but itdoesnot requireanexpressionforf 0.x/, andithasonlyonefunction
call per iteration.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik