6.8 Exercises
169
6.8 Exercises
Exercise 6.1: Hand Calculations for the Trapezoidal Method
Compute by hand the area composed of two trapezoids (of equal width) that approximates the integral
3
1 2x 3 dx. Make a test function that calls the trapezoidal
function in trapezoidal.py and compares the return value with the handcalculated value.
Filename: trapezoidal_test_func.py.
Exercise 6.2: Hand Calculations for the Midpoint Method
Compute by hand the area composed of two rectangles (of equal width) that approximates the integral
3
1 2x 3 dx. Make a test function that calls the midpoint function
in midpoint.py and compares the return value with the hand-calculated value.
Filename: midpoint_test_func.py.
Exercise 6.3: Compute a Simple Integral
Apply the trapezoidal and midpoint functions to compute the integral
6
2 x(x −
1)dx with 2 and 100 subintervals. Compute the error too.
Filename: integrate_parabola.py.
Exercise 6.4: Hand-Calculations with Sine Integrals
We consider integrating the sine 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. Compare with the exact value.
b) Do a) when b = 2π.
Filename: integrate_sine.py.
Exercise 6.5: Make Test Functions for the Midpoint Method
Modify the file test_trapezoidal.py such that the three tests are applied to the
function midpoint implementing the midpoint method for integration.
Filename: test_midpoint.py.
Exercise 6.6: Explore Rounding Errors with Large Numbers
The trapezoidal method integrates linear functions exactly, and this property 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 · 10 8 x − 4 · 10 6 and rerun the test. What happens? How must you change
the test to make it useful? How does the convergence rate test behave? Any need for
adjustment?
Filename: test_trapezoidal2.py.
Précédent

- 190/350

Suivant