Skip to content
K Knidox Search…
Math · Calculus

Numerical Integration

Simpson’s rule against the midpoint and trapezoidal rules, with the error order each one actually achieves.

Use x as the variable, with an explicit * for multiplication. sin, cos, tan, sqrt, ln, log, exp and abs are available.
Simpson’s rule rounds an odd n up to the next even number.
Simpson’s rule
0.78539815

n = 10 · reference 0.78539816 · error 9.913e-9

The three methods at n = 10
MethodEstimateErrorOrder
Midpoint rule0.78560652.08e-4O(h²)
Trapezoidal rule0.78498154.17e-4O(h²)
Simpson’s rule0.785398159.91e-9O(h⁴)
What doubling n does to the error

Midpoint rule: 4.0× · Trapezoidal rule: 4.0× · Simpson’s rule: 63.9×

A second-order method should improve by about 4× and a fourth-order one by about 16×. A ratio well below that usually means the integrand is not smooth enough for the order to hold.

Simpson’s rule fits a parabola through every pair of subintervals instead of a straight line, which makes it dramatically more accurate than the trapezoidal rule for the same work. Its error falls as h⁴, so halving the step size cuts the error roughly sixteen-fold.

Straight lines against parabolas

The trapezoidal rule joins consecutive sample points with a straight line and sums the trapezoids. Simpson’s rule takes them three at a time and fits a parabola through each triple, integrating that exactly. Since a parabola can follow a curve far better than a chord, the same number of function evaluations buys much more accuracy.

The surprising consequence is that Simpson’s rule integrates cubics exactly, even though it only fits quadratics. The cubic part of the error cancels by symmetry across each pair of intervals, which is why the leading error term involves the fourth derivative rather than the third — and why the tool reports “exact for this integrand” rather than a convergence ratio when you give it a polynomial of degree three or less.

What the order actually means

A method of order p has error proportional to hᵖ, where h is the step size. Doubling the number of subintervals halves h, so a second-order method should improve by about 4× and a fourth-order one by about 16×. Watching that ratio is the practical way to check a method is behaving — if Simpson’s rule is only improving fourfold, the integrand is not smooth enough for its order to hold.

∫ f ≈ (h ÷ 3)[f₀ + 4f₁ + 2f₂ + 4f₃ + … + 4fn−1 + fₙ]

Simpson’s rule; the 4s and 2s alternate on interior points and n must be even

  1. 1
    Choose an even number of subintervals. Simpson pairs them up, so n must be even; the tool rounds an odd n up for you.
  2. 2
    Find the step size. For [0, 1] with n = 10, h = 0.1.
  3. 3
    Evaluate the function at every node. At x = 0, 0.1, 0.2, … 1.0 — eleven points for ten intervals.
  4. 4
    Weight them 1, 4, 2, 4, 2, … 4, 1. The endpoints get 1, odd-indexed points get 4 and even-indexed interior points get 2.
  5. 5
    Multiply the total by h ÷ 3. For 1/(1 + x²) on [0, 1] this gives 0.78539815, against the true π/4 = 0.78539816.

Error order by method

h is the step size. The right-hand column is what doubling the number of subintervals should do.

MethodError orderImprovement when n doubles
Left or right rectanglesO(h)About 2×
Trapezoidal ruleO(h²)About 4×
Midpoint ruleO(h²)About 4×, with a smaller constant
Simpson’s ruleO(h⁴)About 16×

When the order stops holding

Every one of these orders assumes the integrand is smooth enough — Simpson’s h⁴ result requires a bounded fourth derivative across the interval. Give it √x on [0, 1], whose derivative blows up at the origin, and all three methods degrade to roughly 2.8× improvement per doubling instead of 4× and 16×. The tool prints the observed ratio precisely so that this failure is visible rather than hidden behind a plausible-looking number.

The same caveat applies to the reference value the errors are measured against. It is Simpson’s rule at 20,000 intervals — extremely accurate for a smooth function, but for an integrand with an endpoint singularity it carries error of its own, so a reported error below about 1e−8 on such a function should be read as “at the limit of this method” rather than as a precise figure. Singular or oscillatory integrands need adaptive quadrature or a substitution, not more subintervals.

Why must n be even for Simpson’s rule?
Because the rule fits a parabola through each consecutive triple of points, consuming two subintervals at a time. An odd count would leave one interval unpaired, so the tool rounds it up to the next even number.
Why is Simpson’s rule so much better than the trapezoidal rule?
It fits parabolas rather than straight lines, so its error depends on the fourth derivative instead of the second. That makes it fourth order: halving the step size cuts the error roughly sixteenfold rather than fourfold.
Why does Simpson’s rule give cubics exactly?
The cubic component of the error cancels by symmetry across each pair of subintervals. So although the rule only fits quadratics, it integrates any polynomial up to degree three with no error at all.
What does error order O(h⁴) mean?
That the error shrinks in proportion to the fourth power of the step size. Halve h and the error falls by a factor of about sixteen — which is the ratio to look for when checking the method is working.
Why is my convergence ratio lower than expected?
Almost always because the integrand is not smooth enough. A function with an unbounded derivative, a kink or a singularity inside the interval breaks the assumption the order rests on, and every method degrades together.
Is the midpoint rule worth using over the trapezoidal rule?
Often, yes. Both are second order, but the midpoint error is about half the size and has the opposite sign, so it is usually closer and the two together bracket the true value.
When should I not use these methods?
For integrands with singularities, sharp peaks or heavy oscillation, and for infinite intervals. Those need adaptive quadrature, a change of variable, or a method designed for the behaviour — adding subintervals will not rescue them.