Unit 3, chapter 13

Approximate an integral with a controlled error

How composite Simpson's rule estimates a definite integral with quadratic interpolants, and how Simpson's error bound with a fourth derivative bound fixes the integral of e to the minus x squared to six decimals.

  • Chapter 13
  • Symmetry, improper integrals, Simpson's rule and power series

The function e−x2e^{-x^2} is smooth and positive on [0,1][0, 1], so the area ∫01e−x2 dx\int_0^1 e^{-x^2}\,dx is a definite number, but none of the usual functions gives an antiderivative to evaluate. Sampling the curve and adding weighted heights gives an estimate, and the real question is how many of its digits can be trusted. This page builds composite Simpson’s rule from parabolas through sample points. It then uses Simpson’s error bound, together with a bound on the fourth derivative, to prove that the integral equals 0.7468240.746824 to six decimal places.

Key terms

No elementary antiderivative

Definition The elementary functions are the functions built from polynomials, roots, exponentials, logarithms, trigonometric functions and their inverses by finitely many arithmetic operations and compositions. A function has no elementary antiderivative when none of its antiderivatives is an elementary function. A theorem of Liouville shows that e−x2e^{-x^2} is such a function, and so are sin⁡xx\frac{\sin x}{x} and 1ln⁡x\frac{1}{\ln x}. Having no elementary antiderivative is not the same as having no antiderivative. If ff is continuous on an interval containing 00, the fundamental theorem of calculus makes F(x)=∫0xf(t) dtF(x) = \int_0^x f(t)\,dt an antiderivative of ff on that interval.

In plain words The area under e−x2e^{-x^2} from 00 to 11 exists and is one definite number. What is missing is a closed formula for an antiderivative, so the number has to be computed another way. The substitution u=−x2u = -x^2 does not help, because du=−2x dxdu = -2x\,dx needs a factor xx that the integrand does not have.

Example With the factor xx present, the antiderivative is elementary,

∫xe−x2 dx=−12e−x2+C.\int x e^{-x^2}\,dx = -\tfrac12 e^{-x^2} + C.

Without it, the antiderivative is usually written with the error function erf⁡(x)=2π∫0xe−t2 dt\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2}\,dt, which gives

∫01e−x2 dx=π2erf⁡(1).\int_0^1 e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2} \operatorname{erf}(1).

The error function is defined by this very integral, so the formula names the number without computing it. Its digits still have to come from an approximation.

Numerical integration

Definition Numerical integration approximates a definite integral ∫abf(x) dx\int_a^b f(x)\,dx by a weighted sum of function values

∑j=0nwjf(xj)\sum_{j=0}^{n} w_j f(x_j)

at finitely many nodes xjx_j in [a,b][a, b], with weights wjw_j fixed by the rule. Riemann sums, the trapezoidal rule and Simpson’s rule are rules of this kind. A numerical value is an approximation until an error bound, or another proof, says how close it is.

In plain words Sample the integrand at a few points, weight the samples and add. Rules differ in where they sample and how they weight. A digit that stays the same as a rule is refined, or two rules that agree, is a useful hint but not a proof, because both can miss the same feature between the samples.

Example On [0,1][0, 1] the function e−x2e^{-x^2} decreases from 11 to e−1≈0.3679e^{-1} \approx 0.3679, so the crudest rules, a single rectangle with the height at either end, already enclose the value,

e−1<∫01e−x2 dx<1.e^{-1} < \int_0^1 e^{-x^2}\,dx < 1.

With two subintervals of width 12\tfrac12, the trapezoidal rule joins the samples at 00, 12\tfrac12 and 11 by straight lines and gives

T2=14(1+2e−1/4+e−1)≈0.731370,\begin{aligned} T_2 &= \tfrac14\bigl(1 + 2e^{-1/4} + e^{-1}\bigr) \\[0.4em] &\approx 0.731370, \end{aligned}

which is too small by about 0.01550.0155. The next card uses the same three samples with different weights and does far better.

Quadratic interpolant

Definition Given three points (x0,f0)(x_0, f_0), (x1,f1)(x_1, f_1) and (x2,f2)(x_2, f_2) with x0<x1<x2x_0 < x_1 < x_2, there is exactly one polynomial pp of degree at most 22 whose graph passes through all three. It is called the quadratic interpolant of the points. When the nodes are equally spaced, with x1−x0=x2−x1=hx_1 - x_0 = x_2 - x_1 = h, its integral is

∫x0x2p(x) dx=h3(f0+4f1+f2).\begin{aligned} &\int_{x_0}^{x_2} p(x)\,dx \\[0.4em] &\quad = \frac{h}{3}\bigl(f_0 + 4f_1 + f_2\bigr). \end{aligned}

This is Simpson’s rule on a single pair of subintervals. To see the formula, put the middle node at 00 with t=x−x1t = x - x_1 and write p=A+Bt+Ct2p = A + Bt + Ct^2. The odd term BtBt integrates to 00 over [−h,h][-h, h], so the integral is 2Ah+23Ch32Ah + \tfrac23 Ch^3. The interpolation conditions give A=f1A = f_1 and 2Ch2=f0−2f1+f22Ch^2 = f_0 - 2f_1 + f_2, and substituting these values gives the formula.

In plain words Over two neighboring subintervals, replace the curve by the parabola through its three sample points and take the exact area under the parabola. The weights 11, 44 and 11 come from integrating that parabola. They weight the samples in the area formula and do not change the heights themselves. The weights add to 66, so a constant height cc receives the correct area 2hc2hc.

Example For ∫01e−x2 dx\int_0^1 e^{-x^2}\,dx with h=12h = \tfrac12, the samples at 00, 12\tfrac12 and 11 are 11, e−1/4e^{-1/4} and e−1e^{-1}, and

S2=16(1+4e−1/4+e−1)≈0.7471804.\begin{aligned} S_2 &= \tfrac16\bigl(1 + 4e^{-1/4} + e^{-1}\bigr) \\[0.4em] &\approx 0.7471804. \end{aligned}

The integral is about 0.74682410.7468241, so this single parabola is off by about 3.6×10−43.6 \times 10^{-4}, more than 4040 times closer than the trapezoidal estimate from the same three samples.

Composite Simpson’s rule

Definition Let ff be defined on [a,b][a, b], let nn be an even positive integer, and set h=(b−a)/nh = (b - a)/n, xj=a+jhx_j = a + jh and fj=f(xj)f_j = f(x_j) for j=0,1,…,nj = 0, 1, \dots, n. Composite Simpson’s rule applies the one-pair formula to [x0,x2][x_0, x_2], [x2,x4][x_2, x_4] and so on up to [xn−2,xn][x_{n-2}, x_n], and adds the results,

Sn=h3(f0+4f1+2f2+4f3+⋯+2fn−2+4fn−1+fn).\begin{aligned} S_n &= \frac{h}{3}\bigl(f_0 + 4f_1 + 2f_2 \\[0.4em] &\quad + 4f_3 + \dots + 2f_{n-2} \\[0.4em] &\quad + 4f_{n-1} + f_n\bigr). \end{aligned}

The endpoints get weight 11, the odd-numbered nodes get weight 44, and the interior even-numbered nodes get weight 22, because each of them is the shared end of two neighboring pairs. The pattern is written for n≥4n \ge 4, and for n=2n = 2 it is the one-pair formula. The number of subintervals must be even so that every subinterval belongs to exactly one pair. When ff is continuous on [a,b][a, b], the values SnS_n approach ∫abf(x) dx\int_a^b f(x)\,dx as nn grows through even numbers.

In plain words Cover the interval with a chain of parabola patches, one for each pair of neighboring subintervals, and add their areas. An odd number of subintervals would leave one subinterval without a partner.

Example Take the integral from the opening, whose integrand e−x2e^{-x^2} is smooth on [0,1][0, 1], with n=4n = 4 and h=14h = \tfrac14. The samples at 00, 14\tfrac14, 12\tfrac12, 34\tfrac34 and 11 are about 11, 0.9394130.939413, 0.7788010.778801, 0.5697830.569783 and 0.3678790.367879. The two odd-numbered samples add to about 1.5091961.509196, so

S4≈112(1+4(1.509196)+2(0.778801)+0.367879)≈0.746855,\begin{aligned} S_4 &\approx \tfrac{1}{12}\bigl(1 + 4(1.509196) \\[0.4em] &\quad + 2(0.778801) \\[0.4em] &\quad + 0.367879\bigr) \\[0.4em] &\approx 0.746855, \end{aligned}

an error of about 3.1×10−53.1 \times 10^{-5}. With n=100n = 100 the same rule, carried out by a computer, gives S100≈0.7468241329S_{100} \approx 0.7468241329, while a high-precision evaluation of the error function gives ∫01e−x2 dx≈0.7468241328\int_0^1 e^{-x^2}\,dx \approx 0.7468241328. The next card shows how to be sure of such digits without knowing the true value.

Simpson’s error bound

Definition Let ff have a continuous fourth derivative on [a,b][a, b], let MM be a number with ∣f(4)(x)∣≤M\lvert f^{(4)}(x) \rvert \le M for every xx in [a,b][a, b], and let nn be even with h=(b−a)/nh = (b - a)/n. Then composite Simpson’s rule satisfies

∣∫abf(x) dx−Sn∣≤(b−a)h4M180.\begin{aligned} &\left\lvert \int_a^b f(x)\,dx - S_n \right\rvert \\[0.4em] &\quad \le \frac{(b - a)h^4 M}{180}. \end{aligned}

Since h=(b−a)/nh = (b - a)/n, the bound also equals M(b−a)5/(180n4)M(b - a)^5/(180 n^4).

In plain words The bound is a guarantee that holds whenever its hypotheses do, and it needs no knowledge of the true value. It shrinks like h4h^4, so halving hh divides it by 1616. It describes the worst case, and the actual error is often much smaller. When the fourth derivative is not continuous on the whole interval, the theorem does not apply, and the error can be far larger than the formula would suggest.

Example Write I=∫01e−x2 dxI = \int_0^1 e^{-x^2}\,dx. Here b−a=1b - a = 1, and the next card shows that M=76M = 76 is a valid bound. With n=100n = 100,

∣I−S100∣≤76180⋅10−8<4.23×10−9.\begin{aligned} \lvert I - S_{100} \rvert &\le \frac{76}{180} \cdot 10^{-8} \\[0.4em] &< 4.23 \times 10^{-9}. \end{aligned}

So the integral lies between S100−4.23×10−9≈0.7468241287S_{100} - 4.23 \times 10^{-9} \approx 0.7468241287 and S100+4.23×10−9≈0.7468241371S_{100} + 4.23 \times 10^{-9} \approx 0.7468241371. Every number in that interval rounds to 0.7468240.746824, so the integral equals 0.7468240.746824 to six decimal places. The actual error is about 8×10−118 \times 10^{-11}, far below the guarantee.

Fourth derivative bound

Definition A fourth derivative bound for ff on [a,b][a, b] is a number MM with ∣f(4)(x)∣≤M\lvert f^{(4)}(x) \rvert \le M for every xx in [a,b][a, b], not only at the sample points. Any valid MM may be used in Simpson’s error bound. When f(4)f^{(4)} is continuous on [a,b][a, b], the smallest valid choice is the maximum of ∣f(4)∣\lvert f^{(4)} \rvert on [a,b][a, b], and a larger MM is still correct but calls for more subintervals.

In plain words Simpson’s rule integrates every cubic polynomial exactly. On a pair of subintervals centered at 00, the term t3t^3 has integral 00 over [−h,h][-h, h], and the rule also gives h3((−h)3+0+h3)=0\tfrac{h}{3}\bigl((-h)^3 + 0 + h^3\bigr) = 0. So the error depends only on how far ff is from being a cubic near each pair, and the fourth derivative, which is 00 for every cubic, measures that.

Example For f(x)=e−x2f(x) = e^{-x^2}, differentiating four times gives

f(4)(x)=q(x) e−x2,q(x)=16x4−48x2+12.\begin{aligned} f^{(4)}(x) &= q(x)\,e^{-x^2}, \\[0.4em] q(x) &= 16x^4 - 48x^2 + 12. \end{aligned}

On [0,1][0, 1] each power of xx is at most 11 and e−x2≤1e^{-x^2} \le 1, so the triangle inequality gives ∣f(4)(x)∣≤16+48+12=76\lvert f^{(4)}(x) \rvert \le 16 + 48 + 12 = 76. This bound is quick to justify, and it is the one used above. A closer look gives the sharp bound 1212. The fifth derivative −8x(4x4−20x2+15)e−x2-8x(4x^4 - 20x^2 + 15)e^{-x^2} changes sign in (0,1)(0, 1) only at x≈0.9586x \approx 0.9586, so f(4)f^{(4)} falls from 1212 at x=0x = 0 to about −7.42-7.42 and then rises to −20/e≈−7.36-20/e \approx -7.36 at x=1x = 1. To guarantee an error below 10−610^{-6}, the bound M=76M = 76 calls for n=26n = 26, while M=12M = 12 needs only n=18n = 18.

Check yourself

  1. Approximate ∫02x4 dx\int_0^2 x^4\,dx by Simpson’s rule with n=2n = 2, and compare the actual error with Simpson’s error bound.
Show the answer

Here h=1h = 1 and the samples at 00, 11 and 22 are 00, 11 and 1616, so

S2=13(0+4⋅1+16)=203.\begin{aligned} S_2 &= \tfrac13(0 + 4 \cdot 1 + 16) \\[0.4em] &= \tfrac{20}{3}. \end{aligned}

The exact value is 325\tfrac{32}{5}, so the error is 203−325=415≈0.267\tfrac{20}{3} - \tfrac{32}{5} = \tfrac{4}{15} \approx 0.267. The fourth derivative is the constant 2424, so M=24M = 24 and the bound is

2⋅14⋅24180=415.\frac{2 \cdot 1^4 \cdot 24}{180} = \frac{4}{15}.

The bound is attained exactly here, so in general it cannot be lowered.

  1. How many subintervals does composite Simpson’s rule need to guarantee an error below 10−610^{-6} for ∫12dxx\int_1^2 \frac{dx}{x}?
Show the answer

For f(x)=1/xf(x) = 1/x the fourth derivative is f(4)(x)=24/x5f^{(4)}(x) = 24/x^5, which is continuous on [1,2][1, 2] and largest in size at x=1x = 1, so M=24M = 24. With b−a=1b - a = 1 and h=1/nh = 1/n the bound is

24180n4=215n4.\frac{24}{180 n^4} = \frac{2}{15 n^4}.

It is below 10−610^{-6} when n4>215⋅106≈133333n^4 > \tfrac{2}{15} \cdot 10^6 \approx 133333, that is, when n>19.1n > 19.1. The number nn must also be even, so n=20n = 20. The bound for n=20n = 20 is about 8.3×10−78.3 \times 10^{-7}, while n=18n = 18 only guarantees about 1.3×10−61.3 \times 10^{-6}. In fact S20≈0.6931474S_{20} \approx 0.6931474 and ln⁡2≈0.6931472\ln 2 \approx 0.6931472, an actual error of about 1.9×10−71.9 \times 10^{-7}.

  1. A classmate wants ∫01x dx\int_0^1 \sqrt{x}\,dx from Simpson’s rule, first with n=3n = 3 and then with n=4n = 4 together with the error bound. What goes wrong?
Show the answer

With n=3n = 3 the three subintervals cannot be grouped into pairs, so composite Simpson’s rule is not defined. It needs an even number of subintervals. With n=4n = 4 the rule applies. The samples at 00, 14\tfrac14, 12\tfrac12, 34\tfrac34 and 11 are 00, 12\tfrac12, 22\tfrac{\sqrt2}{2}, 32\tfrac{\sqrt3}{2} and 11, and with the weights 1,4,2,4,11, 4, 2, 4, 1

S4=3+2+2312≈0.656526,\begin{aligned} S_4 &= \frac{3 + \sqrt{2} + 2\sqrt{3}}{12} \\[0.4em] &\approx 0.656526, \end{aligned}

compared with the exact value 23≈0.666667\tfrac23 \approx 0.666667, an error of about 0.01010.0101. The error bound cannot certify any of this. The fourth derivative f(4)(x)=−1516x−7/2f^{(4)}(x) = -\tfrac{15}{16} x^{-7/2} is unbounded as x→0+x \to 0^+, so it is not continuous on [0,1][0, 1] and no finite MM exists. The error also shrinks slowly. Quadrupling nn to 1616 and then to 6464 gives errors of about 1.3×10−31.3 \times 10^{-3} and 1.6×10−41.6 \times 10^{-4}, a factor of about 88 each time, while for an integrand that meets the hypotheses the bound shrinks by a factor of 44=2564^4 = 256.

How this lesson connects