Page - 194 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 194 -
Text of the Page - 194 -
194 7 SolvingNonlinearAlgebraicEquations
if return_x_list:
return x_list, iteration_counter
else:
return x, iteration_counter
We cannowmakeacall
x, iter = Newton(f, dfdx, x=1000, eps=1e-6, return_x_list=True)
andgeta listx returned.Withknowledgeoftheexactsolutionxoff(x)=0wecan
computeall theerrorsen andall theassociatedqn valueswith thecompact function
(also foundinnonlinear_solvers.py)
def rate(x, x_exact):
e = [abs(x_ - x_exact) for x_ in x]
q = [log(e[n+1]/e[n])/log(e[n]/e[n-1])
for n in range(1, len(e)-1, 1)]
return q
The error model (7.5) works well for Newton’s method and the secant method.
For thebisectionmethod,however, itworkswell in thebeginning,butnotwhen the
solution is approached.
We cancompute the ratesqn andprint themnicely (print_rates.py),
def print_rates(method, x, x_exact):
q = [’{:.2f}’.format(q_) for q_ in rate(x, x_exact)]
print(method + ’:’)
for q_ in q:
print(q_, " ", end="") # end="" suppresses newline
Theresult forprint_rates(’Newton’, x, 3) is
Newton:
1.01 1.02 1.03 1.07 1.14 1.27 1.51 1.80 1.97 2.00
indicating thatq=2 is the rate forNewton’smethod.A similar computationusing
thesecantmethod,gives the rates
secant:
1.26 0.93 1.05 1.01 1.04 1.05 1.08 1.13 1.20 1.30 1.43
1.54 1.60 1.62 1.62
Here it seems thatq≈1.6 is the limit.
Remark If we in the bisection method think of the length of the current interval
containingthesolutionas theerroren, then(7.5)worksperfectlysinceen+1 = 12en,
i.e.,q=1andC= 12,butifen is thetrueerror |x−xn|, it iseasilyseenfromasketch
that thiserrorcanoscillatebetweenthecurrentinterval lengthandapotentiallyvery
smallvalueasweapproachtheexact solution.Thecorrespondingratesqn fluctuate
widelyandareofno interest.
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