Numerical Integration With the Trapezoid and Simpson's Rules
Approximate integrals from formulas or data with two classic rules, with a full worked example and error bounds.
Key takeaways
- With equal spacing h, the trapezoid rule weights the values 1, 2, 2, …, 2, 1 and multiplies the sum by h/2.
- Simpson's rule needs an even number of subintervals, weights the values 1, 4, 2, 4, …, 4, 1 and multiplies by h/3.
- For smooth functions, halving h cuts the trapezoid error to about a quarter and the Simpson error to about a sixteenth.
- Error bounds depend on derivatives of the function, so rough, spiky or singular integrands need extra care.
Numerical integration estimates a definite integral from values of the function at a list of points. The trapezoid rule joins neighboring points with straight lines and adds the areas underneath. Simpson's rule fits parabolas through groups of three points, and for smooth functions it is usually far more accurate with the same data.
You need these rules when no antiderivative formula exists, when the formula is too unwieldy to use, or when you have only measurements and no formula at all.
The trapezoid rule
Divide [a, b] into n equal pieces of width h = (b − a)/n, with points x₀ = a through xₙ = b, and write fᵢ for f(xᵢ).
Over one piece, the area under the straight segment is the width times the average of the two end heights: h(f₀ + f₁)/2. Add all the pieces. Each interior value is shared by two neighbors, so it appears twice:
Tₙ = (h/2)·(f₀ + 2f₁ + 2f₂ + … + 2fₙ₋₁ + fₙ) This is the average of the left and right Riemann sums. If the graph is concave up, each segment lies above the curve and the estimate is too high. If the graph is concave down, it is too low.
Simpson's rule
A straight line ignores curvature. Take two adjacent pieces instead, with three points, and pass a parabola through them. The area under that parabola works out to (h/3)(f₀ + 4f₁ + f₂). Chaining the pairs together gives:
Sₙ = (h/3)·(f₀ + 4f₁ + 2f₂ + 4f₃ + … + 2fₙ₋₂ + 4fₙ₋₁ + fₙ) (n even) The rule is named for the eighteenth century English mathematician Thomas Simpson, though the idea was known before him.
A pleasant surprise: although it is built from parabolas, Simpson's rule is exact for cubic polynomials too. Try x³ on [0, 2] with n = 2 and h = 1. You get (1/3)(0 + 4 × 1 + 8) = 4, which is the true value. The error from the cubic term on the left half cancels the error on the right.
Where the 1, 4, 1 weights come from
Center two adjacent pieces at zero, so the points are −h, 0 and h, and let the parabola be q(x) = Ax² + Bx + C.
area under q from −h to h = 2Ah³/3 + 2Ch (the Bx term cancels)
= (h/3)(2Ah² + 6C)
q(−h) + 4q(0) + q(h) = 2Ah² + 6C The two expressions match, so the area equals h/3 times the weighted sum. You never have to find A, B or C: the three function values carry all the information needed. Shifting the picture along the x-axis changes nothing, so the formula applies to every pair of pieces.
This also explains the alternating pattern in the long formula. Odd-numbered points are middles of parabolas and get weight 4. Even-numbered interior points are shared by two parabolas, so their weight is 1 + 1 = 2. The two ends belong to only one parabola each.
A worked example
Estimate the integral of e^(−x²) from 0 to 1, a function with no elementary antiderivative. Use n = 4, so h = 0.25.
| x | f(x) (rounded) | Trapezoid weight | Simpson weight |
|---|---|---|---|
| 0 | 1.000000 | 1 | 1 |
| 0.25 | 0.939413 | 2 | 4 |
| 0.50 | 0.778801 | 2 | 2 |
| 0.75 | 0.569783 | 2 | 4 |
| 1.00 | 0.367879 | 1 | 1 |
T₄ = (0.25/2)·(5.943873) ≈ 0.742984
S₄ = (0.25/3)·(8.962265) ≈ 0.746855
reference value ≈ 0.746824 The trapezoid estimate is low by about 0.0038. Simpson's is high by about 0.00003, more than a hundred times better, from exactly the same five function values.
With only two pieces, the trapezoid rule gives about 0.731370, an error of about 0.0155. Going to four pieces divided the error by almost exactly 4.
Hypothetical example: the numbers are made up to show the method, not a quote or an average.
A second example you can check exactly
It is wise to test a method on an integral whose value you know. The integral of 1/x from 1 to 2 equals ln 2, about 0.693147. With n = 4 the function values at 1, 1.25, 1.5, 1.75 and 2 are 1, 0.8, 0.666667, 0.571429 and 0.5.
T₄ = (0.25/2)·(1 + 1.6 + 1.333333 + 1.142857 + 0.5) ≈ 0.697024
S₄ = (0.25/3)·(1 + 3.2 + 1.333333 + 2.285714 + 0.5) ≈ 0.693254 The trapezoid error is about 0.0039 and Simpson's is about 0.0001. The trapezoid value is too high, as expected, because the graph of 1/x is concave up for positive x.
To carry out either rule by hand or in a spreadsheet:
- Choose n, making it even for Simpson's rule, and compute h.
- List the points a, a + h, a + 2h and so on up to b.
- Evaluate the function at each point, keeping more decimals than you want in the answer.
- Multiply by the weights, add, and scale by h/2 or h/3.
- Double n and compare the two results before trusting either.
The midpoint rule and a neat relationship
The midpoint rule evaluates f at the center of each piece and multiplies by the width. For the same example with four pieces, it gives about 0.748747, high by about 0.0019.
Notice that this error is about half the size of the trapezoid error and has the opposite sign. That pattern holds generally for smooth functions and small h. So a weighted average that counts the midpoint result twice should cancel most of the error:
(2·Mₙ + Tₙ)/3 = S₂ₙ This identity is exact. With M₂ ≈ 0.754598 and T₂ ≈ 0.731370, the formula returns 0.746855, our value of S₄. Simpson's rule can therefore be seen as an error-canceling blend of two cruder rules. Pushing this idea further, by combining results at several step sizes, leads to Romberg integration.
Error bounds
For a function with continuous derivatives on [a, b], the following guarantees hold.
| Rule | Error is at most | K or L bounds the size of |
|---|---|---|
| Midpoint | K(b − a)h²/24 | second derivative |
| Trapezoid | K(b − a)h²/12 | second derivative |
| Simpson | L(b − a)h⁴/180 | fourth derivative |
In our example, the second derivative of e^(−x²) is (4x² − 2)e^(−x²), whose size on [0, 1] never exceeds 2. The trapezoid bound with h = 0.25 is 2 × 0.0625/12, about 0.0104. The actual error of 0.0038 sits comfortably inside.
You can also use a bound to plan. To guarantee a trapezoid error below 0.0001 here, you need h²/6 ≤ 0.0001, so h ≤ about 0.0245. That means at least 41 pieces.
Estimating the error when you cannot bound it
Derivative bounds are often hard to find. In practice, people compute with n pieces, then with 2n, and compare.
For the trapezoid rule, the error of the finer result is roughly one third of the difference between the two. For Simpson's rule, it is roughly one fifteenth. These follow from the factors of 4 and 16.
Adaptive methods apply this test piece by piece, subdividing only where the two estimates disagree. Effort then goes to the regions where the function bends sharply. This is an everyday example of numerical analysis trading work against accuracy.
Shrinking h forever does not help forever. Each operation carries a tiny rounding error, and eventually adding more terms stops improving the answer.
Working from data
Suppose a stream gauge or a speed sensor gives you readings. The trapezoid rule works even when the times are unevenly spaced, because each piece can use its own width.
Say a flow rate, in liters per second, reads 2 at t = 0, 5 at t = 10, 6 at t = 15 and 4 at t = 30 seconds. The estimated volume is 10 × 3.5 + 5 × 5.5 + 15 × 5 = 137.5 liters.
The simple Simpson formula requires equal spacing and an even number of pieces. With noisy measurements, its higher accuracy may be illusory, since the uncertainty in the readings dominates. No rule can recover a spike that happened between samples.
Common questions
Is Simpson's rule always better than the trapezoid rule?
No. Its advantage rests on the function having a well behaved fourth derivative. For data with sharp corners, such as a load that switches on and off, the two may be equally poor. And for a smooth periodic function integrated over a whole period with equal spacing, the humble trapezoid rule is remarkably accurate, because the errors from different parts of the cycle cancel.
How many decimal places should I carry?
Keep at least two more than you intend to report, and round only at the end. The weighted sum combines many values, and early rounding can swamp the accuracy a finer grid was supposed to buy.
What if n is odd and I want Simpson's rule?
For equally spaced data with an odd number of pieces, a common solution is to apply the related three-eighths rule, with weights 1, 3, 3, 1 multiplied by 3h/8, to three of the pieces, and the ordinary rule to the rest.
When the rules struggle
- Unbounded integrands. The endpoint value of 1/√x at 0 does not exist. Substitute first, here with x = u², or treat it as an improper integral.
- Corners and jumps. Split the interval at those points so that each part is smooth.
- Rapid oscillation. You need several points per wiggle, or a specialized method.
- Many dimensions. A grid with 10 points per direction needs ten billion points in 10 dimensions. Monte Carlo methods, which average random samples, scale much better there.
Scientific libraries often use Gaussian quadrature, which chooses unequal points cleverly and integrates polynomials of degree up to 2n − 1 exactly with n points. The two classic rules remain the best place to learn what accuracy costs, and they are often all you need.