6.8 Exercises
171
Hint Edit test_trapezoidal.py.
Filename: rectangle_methods.py.
Exercise 6.9: Adaptive Integration
Suppose we want to use the trapezoidal or midpoint method to compute an integral
b
a f (x)dx with an error less than a prescribed tolerance . What is the appropriate
size of n?
To answer this question, we may enter an iterative procedure where we compare
the results produced by n 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_integration can be used easily in future programs you write.
a) Write a function
adaptive_integration(f, a, b, eps, method=midpoint)
that implements the idea above (eps corresponds to the tolerance , and method
can be midpoint or trapezoidal).
b) Test the method on
2
0 x 2 dx and
2
0
√
xdx for = 10 −1 , 10 −10 and write out the
exact error.
c) Make a plot of n versus ∈ [10 −1 , 10 −10 ] for
2
0
√
xdx. Use logarithmic scale
for
Filename: adaptive_integration.py.
Remarks The type of method explored in this exercise is called adaptive, because
it tries to adapt the value of n to meet a given error criterion. The true error can very
seldom be computed (since we do not know the exact answer to the computational
problem), so one has to find other indicators of the error, such as the one here where
the changes in the integral value, as the number of intervals is doubled, is taken to
reflect the error.
Exercise 6.10: Integrating x Raised to x
Consider the integral
I =
4
0
x
x dx .
The integrand x x does not have an anti-derivative that can be expressed in terms of
standard functions (visit http://wolframalpha.com and type integral(x**x,x) to
convince yourself that our claim is right. Note that Wolfram alpha does give you an
answer, but that answer is an approximation, it is not exact. This is because Wolfram
alpha too uses numerical methods to arrive at the answer, just as you will in this
171
Hint Edit test_trapezoidal.py.
Filename: rectangle_methods.py.
Exercise 6.9: Adaptive Integration
Suppose we want to use the trapezoidal or midpoint method to compute an integral
b
a f (x)dx with an error less than a prescribed tolerance . What is the appropriate
size of n?
To answer this question, we may enter an iterative procedure where we compare
the results produced by n 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_integration can be used easily in future programs you write.
a) Write a function
adaptive_integration(f, a, b, eps, method=midpoint)
that implements the idea above (eps corresponds to the tolerance , and method
can be midpoint or trapezoidal).
b) Test the method on
2
0 x 2 dx and
2
0
√
xdx for = 10 −1 , 10 −10 and write out the
exact error.
c) Make a plot of n versus ∈ [10 −1 , 10 −10 ] for
2
0
√
xdx. Use logarithmic scale
for
Filename: adaptive_integration.py.
Remarks The type of method explored in this exercise is called adaptive, because
it tries to adapt the value of n to meet a given error criterion. The true error can very
seldom be computed (since we do not know the exact answer to the computational
problem), so one has to find other indicators of the error, such as the one here where
the changes in the integral value, as the number of intervals is doubled, is taken to
reflect the error.
Exercise 6.10: Integrating x Raised to x
Consider the integral
I =
4
0
x
x dx .
The integrand x x does not have an anti-derivative that can be expressed in terms of
standard functions (visit http://wolframalpha.com and type integral(x**x,x) to
convince yourself that our claim is right. Note that Wolfram alpha does give you an
answer, but that answer is an approximation, it is not exact. This is because Wolfram
alpha too uses numerical methods to arrive at the answer, just as you will in this
