Numerical Integration Lab

Estimate area under a curve or between two functions, and visualize how numerical methods approximate the integral.

Loading interactive simulation...

double the strips and Simpson gains sixteenfold 🖖

Every method here converges; the only question is how fast. Double the strips on e^x over [0, 1] and the left sum's error falls by a factor of 2, midpoint and trapezoid by 4, Simpson by 16. Simpson samples three points per strip where the left sum takes one, so it costs three times the arithmetic. At n = 8 on that integrand it is already six orders of magnitude closer.

zoom in and every curve goes straight 🖖

Numerical integration works by chopping the region under a curve into thin slices and filling each one with a simple shape — a rectangle, a trapezoid, or a little parabola — then adding the pieces up. The trick is that inside one narrow strip even a wild curve looks almost straight, so a crude shape fits surprisingly well. Watch the cyan fill: the thinner you make the slices, the less any single strip can be wrong by, whatever shape you filled it with.

Simpson's rule is older than Simpson 🖖

Johannes Kepler published the panel formula this tool uses for Simpson — S = (Δx/6)(f(xL) + 4f(xM) + f(xR)) — in 1615, to gauge the volume of wine barrels. It is still called the Keplersche Fassregel in German. Thomas Simpson wrote it down in 1743, a century and a quarter later. Kepler needed it because a barrel's bulging side has no simple area formula.

NUMERICAL INTEGRATION THEORY — FIVE WAYS TO MEASURE AN AREA

Choosing a Rule: What Shape Do You Lay Under the Curve?

Every rule here slices [a, b] into n strips and replaces the curve inside each strip with something you can measure exactly. The rules differ only in that replacement shape — a flat line at one end, a flat line at the middle, a sloped line, or a parabola — and that choice is what decides your error and how fast it shrinks as you add strips.

Left rectangles — flat at the left edge h · Σ f(xi)
Right rectangles — flat at the right edge h · Σ f(xi+1)
Midpoint rectangles — flat at the centre h · Σ f(xi + h/2)
Trapezoid rule — a straight line across (h/2) · Σ [f(xi) + f(xi+1)]
Simpson’s rule — a parabola through three points (h/3) · [f0 + 4f1 + 2f2 + … + fn]

01

Left rectangles — flat at the left edge

Shape used: A rectangle per strip, its height taken from the left-hand endpoint of that strip.

Rule: h · Σ f(xi)

Error behaviour: On a rising curve every rectangle sits below it, so the total undershoots. Halving the strip width roughly halves the error.

Open this case: Left, 50 strips
Left rectangles — flat at the left edge. Each strip is a rectangle pinned to its left edge, leaving a wedge of curve uncounted. A rectangle per strip, its height taken from the left-hand endpoint of that strip.
Each strip is a rectangle pinned to its left edge, leaving a wedge of curve uncounted.

02

Right rectangles — flat at the right edge

Shape used: A rectangle per strip, its height taken from the right-hand endpoint instead.

Rule: h · Σ f(xi+1)

Error behaviour: On the same rising curve every rectangle now pokes above it, overshooting by roughly what the left rule lost.

Open this case: Right, 8 strips
Right rectangles — flat at the right edge. Same strips, height taken at the right edge — the bias flips sign. A rectangle per strip, its height taken from the right-hand endpoint instead.
Same strips, height taken at the right edge — the bias flips sign.

03

Midpoint rectangles — flat at the centre

Shape used: A rectangle per strip, its height read from the middle of the strip rather than either end.

Rule: h · Σ f(xi + h/2)

Error behaviour: The overshoot on one half of each strip cancels the undershoot on the other. Halving h cuts the error to about a quarter.

Open this case: Inverse tan
Midpoint rectangles — flat at the centre. Sampling at the centre lets each strip’s two errors cancel against each other. A rectangle per strip, its height read from the middle of the strip rather than either end.
Sampling at the centre lets each strip’s two errors cancel against each other.

04

Trapezoid rule — a straight line across

Shape used: Each strip becomes a trapezoid joining the curve’s value at both ends of the strip.

Rule: (h/2) · Σ [f(xi) + f(xi+1)]

Error behaviour: exp(x) on [0, 1] with n = 8: the chords sit above the convex curve, so the estimate comes out high.

Open this case: Exp curved
Trapezoid rule — a straight line across. Chords across each strip: above a convex curve, below a concave one. Each strip becomes a trapezoid joining the curve’s value at both ends of the strip.
Chords across each strip: above a convex curve, below a concave one.

05

Simpson’s rule — a parabola through three points

Shape used: Strips are taken in pairs, and a parabola is fitted through the three points that span them. Needs an even n.

Rule: (h/3) · [f0 + 4f1 + 2f2 + … + fn]

Error behaviour: sin(x) on [0, π] with only n = 10 already matches the exact value 2 to several decimals.

Open this case: Simpson smooth
Simpson’s rule — a parabola through three points. One parabola spans each pair of strips, hugging the curve far more closely than a chord. Strips are taken in pairs, and a parabola is fitted through the three points that span them. Needs an even n.
One parabola spans each pair of strips, hugging the curve far more closely than a chord.
References (1)
  • Insight block 3 — Simpson's rule, a century before Simpson: J. Kepler, Nova stereometria doliorum vinariorum. Linz, 1615 — the Keplersche Fassregel, derived to gauge the volume of wine barrels.

Problem solved in full

  1. The trapezoid rule at n = 8 on e x 5 steps

    Integrate ex from 0 to 1 with eight trapezoids, and find out exactly how wrong the answer is before comparing it with anything. This is the trapezoid rule at n = 8 on ex over [0, 1] — the one integrand for which the estimate can be written in closed form.

    1. The trapezoid rule averages the two end heights of each strip, so every interior height is counted once and the two outer ones count half. For ex on an evenly spaced grid those heights are not eight unrelated numbers: each is the previous one multiplied by e1/8.

    2. Consecutive powers of r sum as a geometric series, so the seven interior heights collapse into a single quotient. The eighth power of r is e exactly, which is what makes the interval [0, 1] the convenient one to do this on.

    3. Multiplying the bracket by the strip width finishes the estimate. Nothing so far is approximate except the original decision to put straight tops on curved strips.

    4. Now run the same three lines without decimals. The end-point average and the geometric quotient share a factor, and what survives the cancellation is e − 1 — the exact integral — multiplied by a quantity that depends on h alone. The trapezoid rule did not approximate ex here; it scaled the right answer by a factor that knows nothing about the curve.

    5. That factor is x coth x at x = h/2, and it is even in x, so its series starts 1 + x²/3 and has no linear term to worry about. At x = h/2 that reads 1 + h²/12, and the 1/12 every textbook quotes in the trapezoid error bound is this series coefficient and nothing more. The next term, −h⁴/720, is only 3.4 × 10⁻⁷, so the leading term alone fixes the error to six figures.

    Answer

    The tool prints an estimate of 1.720519 against a true value of 1.718282, and the difference of 0.0022368 is what I·h²/12 = 0.0022373 predicted from the grid spacing alone. Both figures were available before ex was evaluated anywhere, which is the real content: the trapezoid error on this integral is a property of h, not a mystery about the integrand. The price of an h² law is steep. Reaching the six decimals the tool prints as true takes 462 strips — 463 heights of ex — while the tool's Simpson rule, which weights each panel's left, middle and right heights 1 : 4 : 1, reads 1.718282 at six panels, or thirteen heights. That factor of thirty-five buys nothing new mathematically; it is a different average of heights the trapezoid rule was already standing next to.

Learning path

Three numerical methods, and where each one gives out

Leads to Euler's method

Example problems

  • Left, 50 strips - Fifty left rectangles, and x² is still short by 0.0795. Endpoint bias does not disappear with n, it only shrinks in proportion to it.
  • Right, 8 strips - Eight right rectangles give 3.1875 where eight left ones give 2.1875. The gap is exactly 1, which is h times f(2) - f(0), whatever the curve does in between.
  • Simpson smooth - sin(x) over [0, π] with ten Simpson panels: 2.00000678 against a true 2. Six correct digits from ten strips.
  • Exp curved - Eight trapezoids on e^x read 1.720519 against 1.718282. The worked problem below writes that estimate in closed form and says exactly why it is high.
  • Inverse tan - 1/(1+x²) is even, so over [-2, 2] the left and right sums are the identical 2.211342 and neither brackets anything. Midpoint reads 2.215772 against a true 2.214297.
  • Between parabola and line - x² and x cross twice inside [-1, 2], so the signed area is smaller than the area between them. Simpson returns exactly 1.5, because the difference is a quadratic.
  • Between sin and cos - sin(x) minus cos(x) over [0, π] with eighteen trapezoids: 1.994920 against an exact 2.
  • Custom - A quadratic minus a line is still a quadratic, and Simpson is exact for those, so the panel reads 9.25 with no error at all.