194
7 Solving Nonlinear Algebraic Equations
if return_x_list:
return x_list, iteration_counter
else:
return x, iteration_counter
We can now make a call
x, iter = Newton(f, dfdx, x=1000, eps=1e-6, return_x_list=True)
and get a list x returned. With knowledge of the exact solution x of f (x) = 0 we can
compute all the errors e n and all the associated q n values with the compact function
(also found in nonlinear_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 the bisection method, however, it works well in the beginning, but not when the
solution is approached.
We can compute the rates q n and print them nicely (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
The result for print_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 that q = 2 is the rate for Newton’s method. A similar computation using
the secant method, 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 that q ≈ 1.6 is the limit.
Remark If we in the bisection method think of the length of the current interval
containing the solution as the error e n , then (7.5) works perfectly since e n+1 =
1
2 e n ,
i.e., q = 1 and C =
1
2 , but if e n is the true error |x−x n |, it is easily seen from a sketch
that this error can oscillate between the current interval length and a potentially very
small value as we approach the exact solution. The corresponding rates q n fluctuate
widely and are of no interest.
Précédent

- 215/350

Suivant