170
6 Computing Integrals and Testing Code
Fig. 6.4 Illustration of the rectangle method with evaluating the rectangle height by either the left
or right point
Exercise 6.7: Write Test Functions for
4
0
√
xdx
We want to test how the trapezoidal function works for the integral
4
0
√
xdx.
Two of the tests in test_trapezoidal.py are meaningful for this integral.
Compute by hand the result of using two or three trapezoids and modify the
test_trapezoidal_one_exact_result function accordingly. Then modify
test_trapezoidal_conv_rate to handle the square root integral.
Filename: test_trapezoidal3.py.
Remarks The convergence rate test fails. Printing out r shows that the actual
convergence rate for this integral is 1.5 and not 2. The reason is that the error in the
trapezoidal method 6 is −(b − a) 3 n −2 f (ξ ) for some (unknown) ξ ∈ [a, b]. With
f (x) =
√
x, f (ξ ) → −∞ as ξ → 0, pointing to a potential problem in the size of
the error. Running a test with a > 0, say
4
0.1
√
xdx shows that the convergence rate
is indeed restored to 2.
Exercise 6.8: Rectangle Methods
The midpoint method divides the interval of integration into equal-sized subintervals
and approximates the integral in each subinterval by a rectangle whose height equals
the function value at the midpoint of the subinterval. Instead, one might use either
the left or right end of the subinterval as illustrated in Fig. 6.4. This defines a
rectangle method of integration. The height of the rectangle can be based on the
left or right end or the midpoint.
a) Write a function rectangle(f, a, b, n, height=’left’) for computing
an integral
b
a f (x)dx by the rectangle method with height computed based on
the value of height, which is either left, right, or mid.
b) Write three test functions for the three unit test procedures described in
Sect. 6.6.2. Make sure you test for height equal to left, right, and mid.
You may call the midpoint function for checking the result when height=mid.
6 http://en.wikipedia.org/wiki/Trapezoidal_rule#Error_analysis.
6 Computing Integrals and Testing Code
Fig. 6.4 Illustration of the rectangle method with evaluating the rectangle height by either the left
or right point
Exercise 6.7: Write Test Functions for
4
0
√
xdx
We want to test how the trapezoidal function works for the integral
4
0
√
xdx.
Two of the tests in test_trapezoidal.py are meaningful for this integral.
Compute by hand the result of using two or three trapezoids and modify the
test_trapezoidal_one_exact_result function accordingly. Then modify
test_trapezoidal_conv_rate to handle the square root integral.
Filename: test_trapezoidal3.py.
Remarks The convergence rate test fails. Printing out r shows that the actual
convergence rate for this integral is 1.5 and not 2. The reason is that the error in the
trapezoidal method 6 is −(b − a) 3 n −2 f (ξ ) for some (unknown) ξ ∈ [a, b]. With
f (x) =
√
x, f (ξ ) → −∞ as ξ → 0, pointing to a potential problem in the size of
the error. Running a test with a > 0, say
4
0.1
√
xdx shows that the convergence rate
is indeed restored to 2.
Exercise 6.8: Rectangle Methods
The midpoint method divides the interval of integration into equal-sized subintervals
and approximates the integral in each subinterval by a rectangle whose height equals
the function value at the midpoint of the subinterval. Instead, one might use either
the left or right end of the subinterval as illustrated in Fig. 6.4. This defines a
rectangle method of integration. The height of the rectangle can be based on the
left or right end or the midpoint.
a) Write a function rectangle(f, a, b, n, height=’left’) for computing
an integral
b
a f (x)dx by the rectangle method with height computed based on
the value of height, which is either left, right, or mid.
b) Write three test functions for the three unit test procedures described in
Sect. 6.6.2. Make sure you test for height equal to left, right, and mid.
You may call the midpoint function for checking the result when height=mid.
6 http://en.wikipedia.org/wiki/Trapezoidal_rule#Error_analysis.
