Seite - 183 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 183 -
Text der Seite - 183 -
7.2 Newton’sMethod 183
Withx0 =1000,wegetx1 ≈500,whichis inaccordancewiththegraphinFig.7.1.
Repeating theprocess,weget
x2 =x1− f(x1)
f ′(x1) ≈250 .
Thegeneral scheme2 ofNewton’smethodmaybewrittenas
xn+1 =xn− f(xn)
f ′(xn) , n=0,1,2,.. . (7.1)
The computation in (7.1) is repeated until f (xn) is close enough to zero. More
precisely,we test if |f(xn)|< , with beinga smallnumber.
We moved from 1000 to 250 in two iterations, so it is exciting to see how
fast we can approach the solution x = 3. A computer program can automate
the calculations. Our first try at implementing Newton’s method is in a function
naive_Newton(foundinnaive_Newton.py):
def naive_Newton(f, dfdx, x, eps):
while abs(f(x)) > eps:
x = x - (f(x))/dfdx(x)
return x
The argument x is the starting value, called x0 in our previous mathematical
description.
Tosolve theproblemx2 =9we alsoneed to implement
def f(x):
return x**2 - 9
def dfdx(x):
return 2*x
print(naive_Newton(f, dfdx, 1000, 0.001))
whichinnaive_Newton.pyis includedbyuseofanextrafunctionandatestblock.
2 The term scheme isoften used as a synonym for method or computational recipe.
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