Seite - 169 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 169 -
Text der Seite - 169 -
6.8 Exercises 169
6.8 Exercises
Exercise6.1:HandCalculationsfor the TrapezoidalMethod
Compute by hand the area composed of two trapezoids (of equal width) that ap-
proximates the integral ∫3
1 2x 3dx.Makea test function thatcalls thetrapezoidal
function in trapezoidal.py and compares the return value with the hand-
calculatedvalue.
Filename:trapezoidal_test_func.py.
Exercise6.2:HandCalculationsfor the Midpoint Method
Computebyhandtheareacomposedof tworectangles(ofequalwidth) thatapprox-
imates the integral ∫3
1 2x 3dx.Makea test functionthatcalls themidpointfunction
inmidpoint.pyandcompares the returnvaluewith thehand-calculatedvalue.
Filename:midpoint_test_func.py.
Exercise6.3:ComputeaSimple Integral
Applythetrapezoidalandmidpointfunctionstocomputethe integral ∫6
2 x(x−
1)dxwith2 and100subintervals.Compute the error too.
Filename:integrate_parabola.py.
Exercise6.4:Hand-Calculationswith Sine Integrals
We consider integrating thesine function: ∫b
0 sin(x)dx.
a) Let b = π and use two intervals in the trapezoidal and midpoint method.
Compute the integral by hand and illustrate how the two numerical methods
approximate the integral.Comparewith the exactvalue.
b) Doa) whenb=2π.
Filename:integrate_sine.py.
Exercise6.5:Make Test Functions for theMidpoint Method
Modify the filetest_trapezoidal.pysuch that the three tests are applied to the
functionmidpoint implementing themidpointmethodfor integration.
Filename:test_midpoint.py.
Exercise6.6:ExploreRounding ErrorswithLargeNumbers
The trapezoidal method integrates linear functions exactly, and this prop-
erty was used in the test function test_trapezoidal_linear in the file
test_trapezoidal.py. Change the function used in Sect.6.6.2 to f(x) =
6 · 108x − 4 · 106 and rerun the test. What happens? How must you change
the test tomakeituseful?Howdoestheconvergencerate testbehave?Anyneedfor
adjustment?
Filename:test_trapezoidal2.py.
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