To this point, we have used only equally spaced nodes to compute integrals. Yet there are problems in which non-uniformly distributed nodes would clearly be more appropriate, as demonstrated in Demo 5.7.1.
We would like an algorithm that automatically detects and reacts to a situation like that in Demo 5.7.1, a trait known as adaptivity.
Ideally, we would like to make adaptation decisions based on the error of the integration result. Knowing the error exactly would be equivalent to knowing the exact answer, but we can estimate it using the extrapolation technique of Numerical integration. Consider the Simpson formula (5.6.15) resulting from one level of extrapolation from trapezoid estimates:
By virtue of higher order of accuracy, Rf(4n) should be more accurate than Sf(4n). Hence, a decent estimate of the error in the better of the two Simpson values is
If ∣E∣ is judged to be acceptably small, we are done. This judgment takes some care. For instance, suppose the exact integral is 1020. Requiring ∣E∣<δ≪1 would be fruitless in double precision, since it would require more than 20 accurate digits. Hence checking the absolute size of the error alone is not appropriate. Conversely, consider the integral
We are likely to sample values of the integrand that are larger than, say, 1/2 in absolute value, so obtaining this very small result has to rely on subtractive cancellation. We cannot hope for more than 4-5 accurate digits, so a strict test of the relative error is also not recommended. In other words, we can seek an error that is small relative to the data (the integrand), which is O(1), but not relative to the answer itself.
Typically, we use both relative and absolute error, stopping when either one is considered small enough. Algebraically, the test is
and independently compute estimates to each of the half-length integrals. Each of these half-sized computations recursively applies Simpson’s formula and the error estimation criterion, making further bisections as necessary. Such an approach is called divide and conquer in computer science: recursively split the problem into easier pieces and glue the results together.
It is typical to use just the minimal formula Sf(4) and its error estimate E to make decisions about adaptivity. A computation of Sf(4) requires three trapezoid estimates Tf(1), Tf(2), and Tf(4). As observed in (5.6.18) and Demo 5.6.3, the five integrand evaluations in Tf(4) are sufficient to compute all of these values.
Function 5.7.1 shows an implementation. It uses five function values to compute three trapezoid estimates with n=1, n=2, and n=4, applying the updating formula (5.6.18) twice. It goes on to find the two Simpson approximations and to estimate the error by (5.7.4).
If the error estimate passes the test (5.7.6), the better Simpson value is returned as the integral over the given interval. Otherwise, the interval is bisected, integrals over the two pieces are computed using recursive calls, and those results are added to give the complete integral.
Although adaptivity and the error estimation that goes with it can be very powerful, they come at some cost. The error estimation cannot be universally perfect, so sometimes the answer will not be as accurate as requested, and sometimes the function will be evaluated more times than necessary. Subtle problems may arise when the integral is a step within a larger computation (see Exercise 6).
⌨ For each integral below, use Function 5.7.1 with error tolerance 10−2,10−3,…,10−12. Make a table of errors and the number of integrand evaluation nodes used, and use a convergence plot as in Demo 5.7.2 to compare to fourth-order accuracy. (These integrals were taken from Bailey et al. (2005).)
(a)∫01xlog(1+x)dx=41
(b)∫01x2tan−1xdx=12π−2+2log2
(c)∫0π/2excosxdx=2eπ/2−1
(d)∫01xlog(x)dx=−94 (Note: Although the integrand has the limiting value zero as x→0, you have to implement the function carefully to return zero as the value of f(0), or start the integral at x=ϵmach.)
(e)∫011−x2dx=4π
⌨ For each integral below: (i) use quadgk to find the value to at least 12 digits; (ii) use Function 5.7.1 to evaluate the integral to a tolerance of 10-8; (iii) compute the absolute error and the number of nodes used; (iv) use the O(h2) term in the Euler–Maclaurin formula (5.6.9) to estimate how many nodes are required by the fixed-stepsize trapezoidal formula to reach an absolute error of 10-8.
(a)∫0.13sech(sin(1/x))dx
(b)∫−0.99ln((x+1)3))dx
(c)∫−ππcos(x3)dx
⌨ An integral such as ∫01x−γdx for γ>0, in which the integrand blows up at one or both ends, is known as an improper integral. It has a finite value if γ<1, despite the singularity. One way to deal with the problem of the infinite value for f(t0) is to replace the lower limit with a small number ε. (A more robust way to handle improper integrals is discussed in Chapter 9.)
Using Function 5.7.1 with a small tolerance, make a log-log plot of the error as a function of ε when γ=2/3, for ϵ=10−15,10−16,…,10−45.
⌨ A curious consequence of our logic in Function 5.7.1 is that the algorithm uses what we believe to be a more accurate, sixth-order answer only for estimating error; the returned value is the supposedly less accurate Sf(2n). The practice of returning the extrapolated Rf(4n) instead is called local extrapolation.
Modify Function 5.7.1 to use local extrapolation and repeat parts (a) and (e) of Exercise 1 above, comparing the observed convergence to both fourth order and sixth order.
(a) Define a function g that approximates erf by applying Function 5.6.1 with n=300. Make a plot of the error g(x)−erf(x) at 500 points in the interval [0,3].
(b) Define another approximation h that applies Function 5.7.1 with error tolerance 10-7. Plot the error in h as in part (a). Why does it look so different from the previous case?
(c) Suppose you wished to find x such that erf(x)=.95 by using rootfinding on one of your two approximations. Why is the version from part (a) preferable?
Bailey, D. H., Jeyabalan, K., & Li, X. S. (2005). A Comparison of Three High-Precision Quadrature Schemes. Experimental Mathematics, 14(3), 317–329. 10.1080/10586458.2005.10128931