Page - 169 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 169 -
Text of the Page - 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, 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