MathsCalculus

Numerical Integration
calculator.

Estimate a definite integral five ways, and see how far off each rule lands.

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.

h\displaystyle h=b−an\displaystyle {}= \frac{b - a}{n}
T\displaystyle T=h2×WT\displaystyle {}= \frac{h}{2} \times W_T
WT\displaystyle W_T=f0+2f1+⋯+2fn−1+fn\displaystyle {}= f_0 + 2 f_1 + \dots + 2 f_{n-1} + f_n
S\displaystyle S=h3×WS\displaystyle {}= \frac{h}{3} \times W_S
WS\displaystyle W_S=f0+4f1+2f2+⋯+4fn−1+fn\displaystyle {}= f_0 + 4 f_1 + 2 f_2 + \dots + 4 f_{n-1} + f_n
S3/8\displaystyle S_{3/8}=3h8×W3/8\displaystyle {}= \frac{3h}{8} \times W_{3/8}
W3/8\displaystyle W_{3/8}=f0+3f1+3f2+2f3+⋯+fn\displaystyle {}= f_0 + 3 f_1 + 3 f_2 + 2 f_3 + \dots + f_n
B\displaystyle B=2h45×WB\displaystyle {}= \frac{2h}{45} \times W_B
WB\displaystyle W_B=7f0+32f1+12f2+32f3\displaystyle {}= 7 f_0 + 32 f_1 + 12 f_2 + 32 f_3
+14f4+⋯+7fn\quad + 14 f_4 + \dots + 7 f_n

Here fif_i 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

e−x2e^{-x^2} has no antiderivative made of ordinary functions, so Maya can't use the usual subtraction. She takes n = 8, which makes h = 0.25.

The bell curve from 0 to 2, n = 8
RuleEstimateDifference
Trapezoid0.881703793.8 × 10⁻⁴
Simpson 1/30.882065511.6 × 10⁻⁵
Simpson 3/80.882059222.2 × 10⁻⁵
Boole0.882082389.9 × 10⁻⁷
Gauss, 5 points0.882078023.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.

Open this example in the tool

Ken checks a hand answer for ln x

Ken integrated by parts and got 2ln⁡2−12 \ln 2 - 1, 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 ln⁡x\ln x 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.

Open this example in the tool

Leo integrates a function that blows up at one end

1x\frac{1}{\sqrt{x}} 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.

Open this example in the tool

Reading your result

Read the order, then test it. An error of order h2h^2 falls about 4 times when you double n, and order h4h^4 falls about 16 times. For sin⁡x\sin x from 0 to π\pi, 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 41+x2\frac{4}{1 + x^2} from 0 to 1 with n = 6, the integral is π\pi, 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.

trapezoid error\displaystyle \text{trapezoid error}≤M2(b−a)312n2\displaystyle {}\le \frac{M_2 (b - a)^3}{12 n^2}
Simpson error\displaystyle \text{Simpson error}≤M4(b−a)5180n4\displaystyle {}\le \frac{M_4 (b - a)^5}{180 n^4}

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 sin⁡(100x)\sin(100x) 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 |x−1||x - 1| 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.

Questions people ask

The trapezoid rule fits a straight line across each strip, so its error depends on the second derivative. Simpson's rule fits a parabola across each pair of strips, and its error depends on the fourth derivative. With small strips, that fourth-order term shrinks much faster.

Sources