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 - 185 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 185 -

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

Text der Seite - 185 -

7.2 Newton’sMethod 185 functionsand import thenaive_Newton function fromnaive_Newton.py). With |x0|≤1.08everythingworksfine. For example,x0 =1.08 leads to six iterations if =0.001: -1.05895313436 0.989404207298 -0.784566773086 0.36399816111 -0.0330146961372 2.3995252668e-05 Adjustingx0 slightly to 1.09givesdivisionbyzero!The approximationscomputed byNewton’smethodbecome -1.09331618202 1.10490354324 -1.14615550788 1.30303261823 -2.06492300238 13.4731428006 -1.26055913647e+11 Thedivisionbyzero is causedbyx7 =−1.26055913647×1011, because tanh(x7) is 1.0 to machine precision, and then f ′(x) = 1− tanh(x)2 becomes zero in the denominator inNewton’smethod. The underlying problem, leading to the division by zero in the above example, is that Newton’s method diverges: the approximations move further and further away from x = 0. If it had not been for the division by zero, the condition in thewhile loop would always be true and the loop would run forever. Divergence of Newton’s method occasionally happens, and the remedy is to abort the method whena maximumnumberof iterations is reached. Another disadvantage of the naive_Newton function is that it calls the f(x) function twice as many times as necessary. This extra work is of no concern when f(x) is fast toevaluate,but in large-scaleindustrialsoftware,onecall tof(x)might take hoursor days, and then removingunnecessarycalls is important.The solution inour function is to store thecallf(x) in avariable (f_value) andreuse the value insteadofmakinga newcallf(x). To summarize, we want to write an improved function for implementing Newton’smethodwherewe • handledivisionbyzeroproperly • allowa maximumnumberof iterations • avoid theextraevaluationoff(x) A more robustandefficientversionof the function, inserted ina completeprogram (Newtons_method.py)for solvingx2−9=0, is listedbelow. import sys def Newton(f, dfdx, x, eps): f_value = f(x)
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