Page - 193 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 193 -
Text of the Page - 193 -
6.2 Newton’sMethod 193
Whynotuseanarrayforthexapproximations?
Newton’smethod isnormally formulatedwithan iteration indexn,
xnC1 Dxn f.xn/
f 0.xn/ :
Seeingsuchan index,manywould implement this as
x[n+1] = x[n] - f(x[n])/dfdx(x[n])
Suchanarray isfine, but requires storageof all the approximations. In large
industrial applications,whereNewton’smethod solvesmillions of equations at
once,onecannot afford to storeall the intermediateapproximations inmemory,
so then it is important to understand that the algorithminNewton’smethodhas
nomoreneed forxnwhenxnC1 is computed. Therefore,wecanworkwith one
variablexandoverwrite thepreviousvalue:
x = x - f(x)/dfdx(x)
Running naive_Newton(f, dfdx, 1000, eps=0.001) results in the ap-
proximate solution 3.000027639. A smaller value of epswill produce a more
accurate solution. Unfortunately, the plain naive_Newton function does not re-
turn how many iterations it used, nor does it print out all the approximations
x0;x1;x2;: : :, whichwould indeedbe a nice feature. Ifwe insert such a printout,
a rerun results in
500.0045
250.011249919
125.02362415
62.5478052723
31.3458476066
15.816483488
8.1927550496
4.64564330569
3.2914711388
3.01290538807
3.00002763928
Weclearly see that the iterations approach the solution quickly. This speed of
thesearch for thesolution is theprimarystrengthofNewton’smethodcompared to
othermethods.
6.2.2 MakingaMoreEfficientandRobustImplementation
Thenaive_Newton functionworksfine for the examplewe are considering here.
However, formore general use, there are some pitfalls that should be fixed in an
improvedversionof the code. An examplemay illustratewhat the problem is: let
us solve tanh.x/ D 0, which has solution x D 0. With jx0j 1:08 everything
worksfine. Forexample,x0 leads tosix iterations if D0:001:
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