Seite - 171 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 171 -
Text der Seite - 171 -
6.8 Exercises 171
Hint Edittest_trapezoidal.py.
Filename:rectangle_methods.py.
Exercise6.9:AdaptiveIntegration
Supposewe want to use the trapezoidalormidpointmethod to computean
integral∫b
a f(x)dxwith an error less than a prescribed tolerance . What is the appropriate
sizeofn?
Toanswer thisquestion,we mayenteran iterativeprocedurewherewe compare
the results produced byn and 2n intervals, and if the difference is smaller than ,
the value corresponding to 2n is returned. Otherwise, we halve n and repeat the
procedure.
Hint It may be a good idea to organize your code so that the function
adaptive_integrationcanbeusedeasily in futureprogramsyouwrite.
a) Write a function
adaptive_integration(f, a, b, eps, method=midpoint)
that implements the idea above (eps corresponds to the tolerance , and method
canbemidpointortrapezoidal).
b) Test themethodon ∫2
0 x 2dx and ∫2
0 √
xdx for =10−1,10−10 andwriteout the
exacterror.
c) Make a plot ofnversus ∈ [10−1,10−10] for∫20 √
xdx. Use logarithmicscale
for .
Filename:adaptive_integration.py.
Remarks The typeofmethodexplored in this exercise is called adaptive, because
it tries toadapt thevalueofn tomeetagivenerrorcriterion.Thetrueerrorcanvery
seldom be computed (since we do not know the exact answer to the computational
problem),soonehas tofindother indicatorsof theerror,suchas theoneherewhere
the changes in the integral value, as the number of intervals is doubled, is taken to
reflect theerror.
Exercise6.10:Integratingx Raisedtox
Consider the integral
I = ∫ 4
0 xxdx.
The integrandxx doesnot have an anti-derivative that can be expressed in termsof
standard functions (visit http://wolframalpha.comand typeintegral(x**x,x) to
convinceyourself that ourclaimis right.Note thatWolframalphadoesgiveyouan
answer,but thatanswerisanapproximation,it isnotexact.This isbecauseWolfram
alpha too uses numerical methods to arrive at the answer, just as you will in this
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