Page - 184 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 184 -
Text of the Page - 184 -
184 7 SolvingNonlinearAlgebraicEquations
WhyNotUsean Arrayfor thexApproximations?
Newton’smethod isnormally formulatedwithan iteration indexn,
xn+1 =xn− f(xn)
f ′(xn) .
Seeingsuchan index,manywould implement this as
x[n+1] = x[n] - f(x[n])/dfdx(x[n])
Such an array is fine, but requires storage of all the approximations. In large
industrial applications, where Newton’s method solves millions of equations
at once, one cannot afford to store all the intermediate approximations in
memory, so then it is important to understand that the algorithm in Newton’s
method has no more need forxn whenxn+1 is computed. Therefore, we can
workwithonevariablexandoverwrite thepreviousvalue:
x = x - f(x)/dfdx(x)
Runningnaive_Newton(f, dfdx, 1000, eps=0.001)resultsintheapprox-
imate solution 3.000027639.A smaller value ofepswill produce a more accurate
solution. Unfortunately, the plain naive_Newton function does not return how
many iterations it used, nor does it print out all the approximationsx0,x1,x2,.. .,
whichwould indeedbeanice feature. Ifwe insert suchaprintout(print(x) in the
while loop), a rerunresults in
500.0045
250.011249919
125.02362415
62.5478052723
31.3458476066
15.816483488
8.1927550496
4.64564330569
3.2914711388
3.01290538807
3.00002763928
We clearly see that the iterations approach the solution quickly. This speed of the
search for the solution is the primary strength of Newton’s method compared to
othermethods.
7.2.2 MakingaMoreEfficientandRobustImplementation
Thenaive_Newton function works fine for the example we are considering here.
However, for more general use, there are some pitfalls that should be fixed in an
improvedversionof thecode.An examplemay illustratewhat theproblemis.
Let us use naive_Newton to solve tanh(x) = 0, which has solution x = 0
(interactively,youmaydefinef(x)= tanh(x)andf ′(x)=1−tanh2(x)asPython
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