Skip to article frontmatterSkip to article content

Adaptive integration

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 Example 5.7.1.

We would like an algorithm that automatically detects and reacts to a situation like that in Example 5.7.1, a trait known as adaptivity.

5.7.1Error estimation

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:

Sf(2n)=13[4Tf(2n)Tf(n)]. S_f(2n) = \frac{1}{3} \Bigl[ 4 T_f(2n) - T_f(n) \Bigr].

We expect this method to be fourth-order accurate, i.e.,

abf(x)dx=Sf(2n)+O(n4), \int_a^b f(x)\, dx = S_f(2n) + O(n^{-4}),

We can further extrapolate to sixth-order accuracy using (5.6.17):

Rf(4n)=115[16Sf(4n)Sf(2n)]. R_f(4n) = \frac{1}{15} \Bigl[ 16 S_f(4n) - S_f(2n) \Bigr].

By virtue of higher order of accuracy, Rf(4n)R_f(4n) should be more accurate than Sf(4n)S_f(4n). Hence, a decent estimate of the error in the better of the two Simpson values is

E=Rf(4n)Sf(4n)=Sf(4n)Sf(2n)15. E = R_f(4n) - S_f(4n) = \frac{S_f(4n) - S_f(2n)}{15}.

5.7.2Divide and conquer

If E|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|E| < \delta\ll 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

1062π2sinxdx1012. \int_{10^{-6}}^{2\pi} 2 \sin x\, dx \approx -10^{-12}.

We are likely to sample values of the integrand that are larger than, say, 1/21/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)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

E<δa+δrSf(n), |E| < \delta_a + \delta_r |S_f(n)|,

where δa\delta_a and δr\delta_r are given absolute and relative error tolerances, respectively.

When E|E| fails to meet (5.7.6), we bisect the interval [a,b][a,b] to exploit the identity

abf(x)dx=a(a+b)/2f(x)dx+(a+b)/2bf(x)dx, \int_a^b f(x)\, dx = \int_a^{(a+b)/2} f(x)\, dx + \int_{(a+b)/2}^b f(x)\, dx,

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.

5.7.3Implementation

It is typical to use just the minimal formula Sf(4)S_f(4) and its error estimate EE to make decisions about adaptivity. A computation of Sf(4)S_f(4) requires three trapezoid estimates Tf(1)T_f(1), Tf(2)T_f(2), and Tf(4)T_f(4). As observed in (5.6.18) and Example 5.6.3, the five integrand evaluations in Tf(4)T_f(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=1n=1, n=2n=2, and n=4n=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 5.7.6).

5.7.4Exercises

References
  1. 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