Calculator · Integrals
Trapezoidal rule calculator
Type a function and an interval. The calculator joins the points with straight lines, adds up the trapezoids, and shows how the error falls as you add pieces.
The trapezoidal rule estimates ∫ab f(x) dx by joining the points of the graph at n + 1 equally spaced values of x with straight lines and adding up the areas of the trapezoids underneath. The calculator above does it for any function you type and lists every trapezoid.
The rule is the first numerical integration method most students meet after Riemann sums, and it is a good one: its error falls like 1/n², so ten times as many pieces give about a hundred times less error. This page gives the formula, works an example by hand, derives how many pieces a given accuracy needs, and compares the rule with its neighbours. For the rectangle sums it grows out of, see the Riemann sum calculator.
The formula
With Δx = (b − a)/n and xi = a + iΔx, each trapezoid has parallel sides f(xi−1) and f(xi) and width Δx, so its area is Δx times the average of the two heights. Every interior height is shared by two neighbouring trapezoids, which gives the familiar 1, 2, 2, …, 2, 1 pattern[1]:
It is also exactly the average of the left and right Riemann sums, Tn = (Ln + Rn)/2. On a rising function one is too small and the other too big, and averaging cancels most of both errors.
A worked example: π/4
The integral of 1/(1 + x²) from 0 to 1 is arctan 1 = π/4 ≈ 0.7853982, which makes it a good test: the answer is known, and the curve bends. With n = 4, Δx = 0.25 and the heights at 0, 0.25, 0.5, 0.75, 1 are 1, 0.941176, 0.8, 0.64 and 0.5. Then
The error is about −0.0026: the rule comes out too small. That direction is not an accident. On [0, 1] this curve is concave down near 0, bulging above its chords, so the straight tops of the trapezoids cut under it; where a curve is concave up the trapezoids overshoot. Open "Show the work" under the calculator to see the four trapezoids, their end heights and their areas. Doubling to n = 8 gives 0.784747, an error of −0.00065, four times smaller; n = 16 gives an error of −0.00016, four times smaller again.
The error bound, and how to choose n
If |f″(x)| ≤ K on [a, b], the trapezoidal error is at most[1]
For 1/(1 + x²), the second derivative is (6x² − 2)/(1 + x²)³, whose size on [0, 1] is largest at x = 0, where it equals −2. So K = 2 and the bound for n = 4 is 2/(12 × 16) ≈ 0.0104. The actual error, 0.0026, is four times smaller: the bound uses the worst curvature everywhere, while the real curve is that bent only near 0. To guarantee an error below 10−6, solve 2/(12n²) ≤ 10−6: n ≥ √(166,667) ≈ 408.2, so n = 409. The calculator confirms it: with 409 trapezoids the error is about 2.5 × 10−7, inside the guarantee.
The midpoint rule has the same bound with 24 in place of 12, and Simpson’s rule, which fits parabolas instead of lines, has an error bound proportional to 1/n⁴ and involving the fourth derivative. For the π/4 example Simpson with n = 4 is already within about 6 × 10−6 (the error is 6.007 × 10−6), better than the trapezoidal rule manages with 64 pieces.
When the rule struggles
Bound (3) needs a bounded second derivative. For √x on [0, 1] the second derivative is unbounded near 0, and the trapezoidal error falls more slowly than 1/n², roughly like 1/n1.5; try it in the calculator. For a function with a pole inside the interval, such as tan x on [1, 2], there is no finite area at all; the calculator still adds up its trapezoids but reports that the integral diverges.
The rule is old: approximating a curve by chords is as natural as it gets, and the composite formula appears in calculus texts as soon as the definite integral is defined as a limit of sums[2]. What the modern view adds is the error analysis: knowing the rate, 1/n², lets you predict how many pieces you need before you compute a single one[3]. The adaptive method behind the reference value, which spends its effort where the curve bends, is described in the Riemann sums chapter and in Lyness's paper[4].