Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 187 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 187 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 187 -

Bild der Seite - 187 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python