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

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

Image of the Page - 185 -

Image of the Page - 185 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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)
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python