Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 196 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 196 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 196 -

Image of the Page - 196 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python