Calculus · Numerical integration

Numerical integration

Numerical integration estimates ∫ f from a few values of f: on each piece, replace f by a simple curve through those values and add up the signed areas under them. Simpson’s rule uses parabolas; for smooth functions its error eventually falls about 16 times each time the pieces are halved.

Numerical integration in this visualization

Many integrals cannot be worked out from an antiderivative. The antiderivative of e−x2e^{-x^2} exists but has no formula in elementary functions, and measured data has no formula at all. Numerical integration estimates ∫abf(x) dx\int_a^b f(x)\,dx from values of f alone. Cut [a, b] into n panels of width Δx=(b−a)/n\Delta x = (b - a)/n, replace f on each panel by a simple curve p through a few of its values, and add up the signed areas under p:

Simpson’s rule with n panels (mᵢ is the midpoint of panel i)
Sn=Δx6∑i=0n−1[f(xi)+4f(mi)+f(xi+1)]S_n = \frac{\Delta x}{6} \sum_{i=0}^{n-1} \big[f(x_i) + 4f(m_i) + f(x_{i+1})\big]

The midpoint rule M puts a level line through f(mi)f(m_i), the trapezoid rule T the chord between the two ends, and Simpson’s rule S the parabola through the ends and the midpoint, so it uses 2n + 1 values of f. On the plot p is yellow. The strip under the plot draws the gap p(x)−f(x)p(x) - f(x), magnified; its signed area is the error, the sum minus the integral. Textbooks often call this Simpson’s rule with 2n subintervals; it is the same sum.

On a panel of width h, expand f about the midpoint. The midpoint rule’s error is about −h3f′′/24-h^3 f''/24 and the trapezoid rule’s about +h3f′′/12+h^3 f''/12, so on small panels the first is close to −½ times the second: on sin x with n = 4 they are +0.0523 and −0.1039. Weighting them 2 : 1 cancels that leading term, and indeed S=(2M+T)/3S = (2M + T)/3 exactly. What is left starts at h5f(4)/2880h^5 f^{(4)}/2880 per panel. The strip shows it: on every function here the trapezoid’s gap keeps one sign, while Simpson’s has positive and negative parts that nearly cancel.

Simpson’s rule is exact for cubics, not just for quadratics. About the midpoint, (x−m)3(x - m)^3 is odd: its integral over the panel is 0, and so is Simpson’s sum for it, because the weights 1, 4, 1 are symmetric. For x³ on [0, 2] with one panel, the parabola is 3x2−2x3x^2 - 2x, clearly not x³, yet the two lobes of the gap have areas −1/4 and +1/4, so S1=4S_1 = 4, the integral exactly. The term (x−m)4(x - m)^4 is even and does not cancel: for x⁴ one parabola gives 20/3 against 32/5, off by 4/15.

Adding up the panels, the errors scale with powers of 1/n. For a smooth f whose leading error term is not zero, doubling n eventually cuts the midpoint and trapezoid errors about 4 times (error ∝ 1/n²) and Simpson’s about 16 times (∝ 1/n⁴). On eˣ, Simpson’s error goes 5.8 × 10⁻⁴, 3.7 × 10⁻⁵, 2.3 × 10⁻⁶ as n goes 1, 2, 4. The expansion needs f to be smooth. √x starts straight up at 0, no parabola can match that infinite slope, and the error drops only about 21.5≈2.832^{1.5} \approx 2.83 times per doubling, almost all of it from the first panel.

Things to notice

  • The integral in the readout comes from an antiderivative (2, e − 1, ln 2, 2/3, 4, 32/5), so the error shown is the true error, not a guess.
  • For the same 9 values of sin x, Simpson’s rule on four panels misses ∫₀^π sin x dx by 0.00027 and the trapezoid rule on eight panels by 0.026.

Controls

  • Turn n and switch rules to see the gap change. The strip rescales itself each time, so read the scale at its left.
  • Press n → ∞ to double n up to 64. Each row records the error and how many times smaller it got than the row above.

Related