Page - 193 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 193 -
Text of the Page - 193 -
7.5 RateofConvergence 193
A single q in (7.5) is defined in the limit n → ∞. For finiten, and especially
smallern, q will vary withn. To estimateq, we can compute all the errors en and
set up(7.5) for threeconsecutiveexperimentsn−1,n, andn+1:
en=Ceqn−1,
en+1 =Ceqn .
Dividing, e.g., the latter equation by the former, and solving with respect to q, we
get that
q= ln(en+1/en)
ln(en/en−1) .
Since thisq will vary somewhat withn, we call itqn. Asngrows, we expectqn to
approacha limit (qn→q).
ModifyingOurFunctions toReturnAll Approximations To computeall theqn
values,we needall thexn approximations.However,ourprevious implementations
of Newton’smethod, the secant method,and thebisection method returned just the
finalapproximation.
Therefore, we have modified our solvers5 accordingly, and placed them in
nonlinear_solvers.py.A user can choose whether the final value or the whole
history of solutions is to be returned. Each of the extended implementations now
takesanextraparameterreturn_x_list.Thisparameter is aboolean,set toTrue
if the function is supposed to return all the root approximations, or False, if the
functionshouldonly return thefinalapproximation.
Asanexample, letus takea closer lookatNewton:
def Newton(f, dfdx, x, eps, return_x_list=False):
f_value = f(x)
iteration_counter = 0
if return_x_list:
x_list = []
while abs(f_value) > eps and iteration_counter < 100:
try:
x = x - float(f_value)/dfdx(x)
except ZeroDivisionError:
print(’Error! - derivative zero for x = {:g}’.format(x))
sys.exit(1) # Abort with error
f_value = f(x)
iteration_counter += 1
if return_x_list:
x_list.append(x)
# Here, either a solution is found, or too many iterations
if abs(f_value) > eps:
iteration_counter = -1 # i.e., lack of convergence
5 An implemented numerical solution algorithm isoften called a solver.
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