Numerical Integration
calculator.
Estimate a definite integral five ways, and see how far off each rule lands.
- Adaptive reference
- 0.882081390.882081
- Step h
- 0.25
- Exact up to degree
- 3
Simpson’s 1/3 rule lands 0.00001588 from the reference, using 9 values of f. The trapezoid rule is about 24 times further off. Boole’s rule came closest here.
The area, and the curve the rule uses
Drag the point to change bf(x) from x = 0 to x = 2 with the area shaded. Dots mark the 9 points where Simpson’s 1/3 rule samples f, and the second line is the curve the rule integrates in place of f. The reference value is 0.88208139.
All five rules on this interval
| Rule | n used | Order | Estimate | Difference |
|---|---|---|---|---|
| Trapezoidn = 8, error shrinks like O(h²) | 8 | O(h²) | 0.88170379 | 0.0003776 |
| Simpson 1/3n = 8, error shrinks like O(h⁴) | 8 | O(h⁴) | 0.88206551 | 0.00001588 |
| Simpson 3/8n = 9, error shrinks like O(h⁴) | 9 | O(h⁴) | 0.88205922 | 0.00002217 |
| Boolen = 8, error shrinks like O(h⁶) | 8 | O(h⁶) | 0.88208238 | 9.9198 × 10⁻⁷ |
| Gauss, 5 pointsFive points, exact to degree 9 | 5 | degree 9 exact | 0.88207802 | 0.00000337 |
When your n does not fit a rule, it is rounded up to the next value that does. Gauss-Legendre always uses its five points and ignores n.
Working
- 1Step size: the interval split into n equal partsh =b − an=2 − 08= 0.25
- 2Evaluate f at the 9 nodesf(0) = 1, f(0.25) = 0.93941306, f(0.5) = 0.77880078, …, f(2) = 0.01831564
- 3Weight the values 1, 4, 2, 4, …, 2, 4, 1, then multiply by the factorh3× (weighted sum) = 0.88206551
- 4Check: adaptive Gauss–Kronrod, a method that does not depend on nReference = 0.88208139, within 0.00001588 of this estimate
Nodes and weights
| i | xᵢ | f(xᵢ) | Weight |
|---|---|---|---|
| 0 | 0 | 1 | 0.08333333 |
| 1 | 0.25 | 0.93941306 | 0.33333333 |
| 2 | 0.5 | 0.77880078 | 0.16666667 |
| 3 | 0.75 | 0.56978282 | 0.33333333 |
| 4 | 1 | 0.36787944 | 0.16666667 |
| 5 | 1.25 | 0.20961139 | 0.33333333 |
| 6 | 1.5 | 0.10539922 | 0.16666667 |
| 7 | 1.75 | 0.04677062 | 0.33333333 |
| 8 | 2 | 0.01831564 | 0.08333333 |
The weights add up to b − a, and the estimate is the sum of each weight times f at its node.
Simpson 1/3 as n doubles
| n | Estimate | Error | Cut by |
|---|---|---|---|
| 8 | 0.88206551 | 0.00001588 | – |
| 16 | 0.8820804 | 9.9419 × 10⁻⁷ | ÷ 16 |
| 32 | 0.88208133 | 6.2116 × 10⁻⁸ | ÷ 16 |
| 64 | 0.88208139 | 3.9 × 10⁻⁹ | ÷ 16 |
| 128 | 0.88208139 | 2.4 × 10⁻¹⁰ | ÷ 16 |
On a smooth curve, each doubling should cut the trapezoid error about 4 times, the Simpson errors about 16 times and the Boole error about 64 times. A much smaller cut points to a kink, a jump or a vertical asymptote.
How it's worked out
Some integrals have no formula you can write down, and some functions come from a simulation instead of an equation. A quadrature rule still works, because it needs only values of f. It samples f at set points, multiplies each value by a weight, and adds them up.
Four of the rules here split the interval into n equal pieces of width h. On each group of pieces they fit a simple curve through the samples and integrate that curve. The Newton-Cotes formulas differ only in the curve: a line, a parabola, a cubic or a quartic.
Here means f at the point a + ih, and each W adds up the samples with that rule's weights. Simpson's 1/3 rule needs an even n, the 3/8 rule a multiple of 3, and Boole's rule a multiple of 4. If your n does not fit, it is rounded up for that rule, so n = 5 runs the 3/8 rule with n = 6.
Gauss-Legendre also chooses where to sample. Its five nodes on the range from −1 to 1 sit at 0, ±0.538469 and ±0.90618, with weights 0.568889, 0.478629 and 0.236927. That choice makes it exact for any polynomial up to degree 9. It runs once across your interval, so it ignores n.
The reference value comes from adaptive Gauss-Kronrod, a method built on QUADPACK's 15-point rule. It keeps splitting the piece with the largest error estimate until the total estimate is near 10⁻¹¹. It also never samples the ends of the interval.
Worked examples
Maya estimates a bell curve integral for her statistics class
has no antiderivative made of ordinary functions, so Maya can't use the usual subtraction. She takes n = 8, which makes h = 0.25.
| Rule | Estimate | Difference |
|---|---|---|
| Trapezoid | 0.88170379 | 3.8 × 10⁻⁴ |
| Simpson 1/3 | 0.88206551 | 1.6 × 10⁻⁵ |
| Simpson 3/8 | 0.88205922 | 2.2 × 10⁻⁵ |
| Boole | 0.88208238 | 9.9 × 10⁻⁷ |
| Gauss, 5 points | 0.88207802 | 3.4 × 10⁻⁶ |
The reference is 0.88208139. The trapezoid and Simpson 1/3 rules use the same nine values of f, yet the trapezoid rule lands about 24 times further off. The weights, not extra work, make the gain.
Ken checks a hand answer for ln x
Ken integrated by parts and got , which is 0.38629436. He wants a second opinion that does not use his algebra. With n = 4, the trapezoid rule gives 0.38369951 and Simpson's 1/3 rule gives 0.38625956.
The curve bends downward, so each straight top lies under it, and the trapezoid value is too low, as you would expect. The reference of 0.38629436 matches his answer. Had it not, his algebra would be the first thing to recheck.
Leo integrates a function that blows up at one end
has no value at 0, but its area from 0 to 1 is finite. The trapezoid, Simpson and Boole rules all need f at 0, so they stop and say so. Gauss-Legendre avoids the ends, and it gives 1.8416.
That is not close to the true value of 2. A curve this steep near one end is hard for a single fit. The adaptive reference keeps splitting pieces near 0 and reaches 2, after 1,995 evaluations of f.
Reading your result
Read the order, then test it. An error of order falls about 4 times when you double n, and order falls about 16 times. For from 0 to , going from n = 16 to n = 32 cuts the trapezoid error 4 times and the Simpson error 16.06 times. If your own ratios look nothing like these, f is probably not smooth on your interval.
Agreement is evidence, not proof. When several rules and the reference agree to many digits, you can trust those digits. The reference is numerical too, so it comes with its own error estimate.
Which rule to pick. On a smooth curve with one fixed set of samples, Boole's rule usually beats Simpson's, and Simpson's beats the trapezoid rule. For from 0 to 1 with n = 6, the integral is , and the trapezoid rule misses by 4.6 × 10⁻³ while Simpson's 1/3 rule misses by 8.7 × 10⁻⁷.
Bounds from the textbook. OpenStax gives these limits, with M the largest size of the named derivative on the interval. The real error is often well below them.
Limits. Values use double precision, about 15 to 16 significant digits, so a difference near 10⁻¹³ is rounding noise and not real error. Infinite limits are refused here, and the improper integral calculator handles those. Reversed limits are fine and flip the sign.
Common mistakes
- Trusting a too-coarse n because the answers agree. For from 0 to 1, Simpson's rule gives −0.2593 at n = 4 and −0.2593 at n = 8. Those look settled, but the reference is 0.0014. The samples miss whole waves, so h must be much smaller than one wave.
- Running across a kink. The error formulas assume f is smooth. For from 0 to 2.5 with n = 8, the trapezoid rule misses by 1.6 × 10⁻². Split the integral at the kink, and integrate each smooth piece on its own.
- Ignoring the n that was used. The rules can run with different n, because each one rounds up to a value it accepts. Compare rules by the number of values of f they use, not by the n you typed.
- Using degrees. Trigonometric functions here take radians. An upper limit of 180 means 180 radians and not a half turn, so type π instead.