The trapezoid rule
Lesson 2.5.1 gave each strip a flat top, at the height of one point. The trapezoid rule joins the heights at the two edges of the strip with a straight line instead. Each strip becomes a trapezoid, whose area is the average of its two heights times its width.
As in lesson 2.5.1, cut into strips of width , with edges . Write , and for the left, right and midpoint sums, and for the trapezoid rule.
Rule: Trapezoid rule
Every inner edge belongs to two strips, so its height counts in full; the two outer edges count half. The left sum uses the heights at to , and the right sum those at to . Their average weights the heights the same way.
Like any area, is in height units times width units. Take on with 4 strips. The heights at the edges are and 1, so
That is the average of lesson 2.5.1's left sum 0.21875 and right sum 0.46875. The exact area is , so is too large by 0.0104. For on , the left and right sums 0.759524 and 0.634524 average to 0.697024, against the exact .
The trapezoid rule with 4 strips on over . The curve bends down, so every straight top runs under it and the total, 1.8961, is short of 2.
Try it
trapezoid_rule is the rule above: half weights at the two ends, full weights inside. riemann is lesson 2.5.1's sum.
The cell prints for and for , each beside the left and right sums and their average. The last line is the error for .
The trapezoid error, and the midpoint's
The trapezoid error shrinks like , as the midpoint error does (lesson 2.5.1). With 10 strips, against the exact integrals of lesson 2.5.1:
| integral | midpoint error | trapezoid error |
|---|---|---|
| on | ||
| on | ||
| on | ||
| on |
In every row the midpoint error is about half the trapezoid error, with the opposite sign. With 100 strips each error is a hundred times smaller.
The sign comes from the bend (lesson 2.4.2). Where the curve bends up, each straight top lies above it, so the trapezoid rule is too large.
The midpoint rectangle has the same area as the strip under the tangent at the midpoint. That tangent lies below the curve, so the midpoint sum is too small.
Rule: Trapezoid and midpoint errors
is the exact integral. is the integral of (lesson 2.5.3): the total bend over . Bending up makes it positive, so the trapezoid rule is too large and the midpoint sum too small, by half as much.
For on , , so the rule gives for 4 strips: exactly the error found above. For on the total bend is . So the trapezoid rule is too small there, as the figure above shows, and the midpoint sum too large.
Whether rises or falls does not matter, unlike for the left and right sums: only the bend does. A straight line has no bend, so both rules are exact for it.
Try it
bend is for each example. Each line prints the midpoint error, the trapezoid error and the rule's prediction for the trapezoid, with 10 strips and then 100.
Simpson's rule
Straight tops cannot follow a bend. Simpson's rule replaces them by curves: over each pair of strips, the parabola through the three heights.
Over one pair, from to , that parabola has area , where is the width of one strip. Adding the pairs gives the weights. The middle of each pair counts 4, an edge shared by two pairs counts , and the two ends count 1.
Simpson's rule with one pair of strips on : . Three points already give the area to within 5%.
Rule: Simpson's rule
Weights 1, 4, 2, 4, …, 2, 4, 1, times . The number of strips must be even, so that the strips pair up.
For on with 4 strips, and the heights at 1, 1.25, 1.5, 1.75 and 2 are 1, 0.8, 0.666667, 0.571429 and 0.5. So
It is off by . Lesson 2.5.1's midpoint sum with the same 4 strips, 0.69122, is off by : eighteen times as much, the other way.
Try the rules side by side below, starting from these four strips of . Switch between midpoints, trapezoids and Simpson's parabolas, then raise to 10 and 100 and compare how fast each error shrinks.
Explore
Add strips and change how each strip's top is drawn: flat, a straight line (trapezoids) or a parabola through each pair (Simpson). Compare how fast each error shrinks.
- Width of each
- 1 ÷ 4 = 0.25
- Total of 4 strips
- 0.69325397
- Exact area
- ln 2 = 0.693147
- Error
- 0.000107 (an overestimate)
Try it
SymPy fits the parabola through three heights , , at 0, and , and integrates it: the area is .
simpson_rule builds the weights 1, 4, 2, …, 4, 1 and refuses an odd number of strips. The last line compares for with lesson 2.5.1's .
Simpson's rule from the other two
The midpoint error is about times the trapezoid error. So in the two errors cancel, to leading order.
Rule: Simpson from midpoint and trapezoid
With strips, the trapezoid rule uses the edges and the midpoint sum the middles. Together they are the points of Simpson's rule with strips, and the weights work out to 1, 4, 2, …, 4, 1.
For on with : , which is , off by only .
Combining two estimates so that their leading errors cancel is called Richardson extrapolation. Halving the strips cuts the trapezoid error about 4 times, so gives as well.
Its error shrinks like
Simpson's rule beats the midpoint sum on lesson 2.5.1's examples, and by more as the strips get thinner:
| integral | error | error | error | error |
|---|---|---|---|---|
| on | 0 | 0 | ||
| on | ||||
| on | ||||
| on |
From 10 to 100 strips, Simpson's error for falls from to : ten times as many strips, ten thousand times smaller. On a log-log chart that is a slope of , against for the other two rules.
Rule: Simpson's error
The error shrinks like . For a cubic, is a constant, so and Simpson's rule is exact.
A parabola cannot follow a cubic, yet the errors on the two halves of each pair cancel. For , two strips give exactly. For they give 6.666667 against the exact 6.4, off by , just as the rule says.
The size of each rule's error for . The trapezoid and midpoint lines are parallel, a factor of 2 apart; Simpson's line is twice as steep, until rounding stops it near .
Try it
The first loop checks and against for . The second prints the table: midpoint and Simpson errors with 4 and 100 strips.
With 100 strips Simpson's error for prints as : the rule is exact, and only rounding is left.
Where the rules break
The error rules assume that is smooth on the whole of , and Simpson's rule needs pairs of strips. Three things break them.
A corner
has a corner at 0.37, and its exact integral over is . A parabola cannot bend sharply, so the pair of strips holding the corner gets an error of order :
| strips | midpoint error | trapezoid error | Simpson error |
|---|---|---|---|
| 10 | |||
| 20 | |||
| 100 |
With 100 strips Simpson's rule is worse than both of the others. Its error jumps about with the position of the corner inside its pair.
The fix is to split the integral at the corner and apply the rule to each smooth piece. With 100 strips on each piece, the error is .
Case study 2.6.5 split an option-pricing integral at the corner of its payoff for this reason.
A curve that blows up at an end
is an improper integral (lesson 2.6.3). The trapezoid and Simpson rules both need the height at , and does not exist: Python raises ZeroDivisionError.
The midpoint sum never touches the ends, so it runs, but slowly. Its error is with 100 strips and with 10,000: a hundred times the strips for ten times the accuracy.
Remove the blow-up first, with a substitution such as (lesson 2.6.1), which turns this integral into . Or use quad, which handles such ends.
An odd number of strips
Simpson's weights need pairs. Applied blindly to 3 strips of on , the pattern gives weights 1, 4, 2, 1 and the answer against . No error message warns you.
With data at an even number of points, which makes an odd number of strips, use one of the fixes in the next section.
Try it
The first loop prints the three errors for the corner at 0.37, then Simpson's error after splitting there.
Next the blow-up at 0: the first height the trapezoid rule needs, then the midpoint sum. Last, the 1, 4, 2, 1 weights on three strips of .
SciPy's trapezoid and simpson
scipy.integrate has both rules. They take the heights, not the function: trapezoid(y, x=x) and simpson(y, x=x), where x holds the points and y the values there. That suits data you already have, such as a table measured at fixed times.
The points need not be evenly spaced. simpson also accepts an odd number of strips: it treats the last strip with a special formula, so its answer stays accurate.
For a function you can evaluate anywhere, quad (lesson 2.5.3) is usually the better tool: it picks its own points and reports an error estimate.
Try it
For on 11 points (10 strips), SciPy gives and . They are plus the errors found above, and .
On 10 points, 9 strips, simpson still gives to within .
Common mistakes
- The trapezoid rule halves the two end heights. Counting them in full adds too much.
- Simpson's weights are 1, 4, 2, …, 4, 1, times , where is the width of one strip, not of a pair.
- Simpson's rule needs an even number of strips. The pattern applied to an odd number gives a wrong answer with no warning.
- is not Simpson's rule. The midpoint error is half the trapezoid's, so the weights are 2 and 1.
- A corner or a blow-up spoils the error rules. Split at a corner; remove a blow-up at an end, or use
quad. trapezoidandsimpsontake values, not a function. Evaluate at the points first.
Trapezoid and Simpson rules
0 of 11 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- Trapezoid rule: .
- and : the midpoint error is about half the trapezoid's, with the opposite sign.
- Simpson's rule: weights 1, 4, 2, …, 4, 1 times , with even. .
- Simpson's error shrinks like , and is zero for cubics.
- Split at a corner, remove a blow-up at an end, and never apply Simpson's weights to an odd number of strips.
scipy.integrate.trapezoid(y, x=x)andsimpson(y, x=x)take values;quadtakes a function.