Numerical integration

Not every integral has a closed-form antiderivative, and in engineering practice most integrals arrive as tabulated data from sensors or experiments rather than as formulas at all. Numerical integration — also called numerical quadrature — approximates ∫abf(x) dx\int_a^b f(x)\,dx using only a finite set of function values, turning an analytic problem into arithmetic on a table. This topic follows finite differences and interpolation in the Numerical Analysis block of MA10002: both standard rules covered here are, underneath, exact integrals of a piecewise polynomial fitted through the tabulated points. The trapezoidal rule fits straight lines; Simpson's 1/31/3 rule fits parabolas. Knowing which to use, when each applies, and how to bound the error is the practical skill this article builds.

The core insight

Approximate ∫abf(x) dx\int_a^b f(x)\,dx by replacing ff with a piecewise polynomial through equally spaced tabulated points and integrating that polynomial exactly — a straight line per panel gives the trapezoidal rule, a parabola per pair of panels gives Simpson's 1/31/3 rule, and the parabola is more accurate for the same spacing.

The core idea

Divide [a,b][a,b] into nn equal subintervals of width h=b−anh = \dfrac{b-a}{n}, with nodes x0=a,x1,…,xn=bx_0 = a, x_1, \dots, x_n = b and function values fi=f(xi)f_i = f(x_i). Both rules replace the unknown curve ff on small pieces of [a,b][a,b] by a polynomial that agrees with ff at the tabulated nodes, then integrate that polynomial exactly — the arithmetic below is just the result of doing that integration once and for all, so it never has to be repeated by hand.

Trapezoidal rule. On each subinterval [xi−1,xi][x_{i-1},x_i], ff is replaced by the straight line joining (xi−1,fi−1)(x_{i-1},f_{i-1}) and (xi,fi)(x_i,f_i), so the area under ff is approximated by a trapezoid of area h2(fi−1+fi)\dfrac{h}{2}(f_{i-1}+f_i). Summing over all nn panels and noticing every interior ordinate is shared by two adjacent trapezoids gives the composite formula:

∫abf(x) dx≈h2[f0+2(f1+f2+⋯+fn−1)+fn]\int_a^b f(x)\,dx \approx \frac{h}{2}\Big[f_0 + 2(f_1+f_2+\cdots+f_{n-1}) + f_n\Big]

where: hh is the panel width, f0f_0 and fnf_n are the endpoint values (weight 11), and every interior ordinate f1,…,fn−1f_1,\dots,f_{n-1} carries weight 22 because it belongs to two panels. This formula works for any n≥1n \ge 1 — there is no parity restriction.

Simpson's 1/31/3 rule. Take the nodes three at a time and fit a parabola through each triple (xi−1,fi−1),(xi,fi),(xi+1,fi+1)(x_{i-1},f_{i-1}), (x_i,f_i), (x_{i+1},f_{i+1}); integrating that parabola exactly over the pair of panels it spans gives a much better local fit than a straight line for smooth ff. Because nodes are consumed two panels at a time, this requires nn to be even:

∫abf(x) dx≈h3[f0+4(f1+f3+⋯+fn−1)+2(f2+f4+⋯+fn−2)+fn]\int_a^b f(x)\,dx \approx \frac{h}{3}\Big[f_0 + 4(f_1+f_3+\cdots+f_{n-1}) + 2(f_2+f_4+\cdots+f_{n-2}) + f_n\Big]

where: odd-indexed ordinates (f1,f3,…f_1,f_3,\dots) are the midpoints of each parabolic panel and carry weight 44; even-indexed interior ordinates (f2,f4,…f_2,f_4,\dots) are shared endpoints between adjacent panels and carry weight 22; the first and last ordinates always carry weight 11. A useful qualitative fact, not proved here but worth knowing for MA10002: Simpson's rule is exact even for cubic polynomials, not just quadratics, because the symmetric 13,43,13\tfrac{1}{3},\tfrac{4}{3},\tfrac{1}{3} weighting makes the leading cubic error term cancel over a symmetric panel. This is why Simpson's rule generally converges faster than the trapezoidal rule as hh shrinks — it captures curvature that a straight-line fit simply cannot see. The trapezoidal rule, by contrast, is exact only for linear functions, but it has no restriction on nn and remains the fallback whenever the data gives an odd number of intervals.

Figure
Trapezoidal panels use straight lines between points while Simpson panels fit a parabola through three points, with the parabola tracking a curved function more closely
Straight-line panels versus parabolic panels trapezoidal rule Simpson's 1/3 rule straight segments miss the curvature parabola tracks the true curve closely
Derivation ADeriving the trapezoidal rule from a single panel

The goal is to obtain the single-panel trapezoidal area h2(fi−1+fi)\dfrac{h}{2}(f_{i-1}+f_i) from first principles by fitting a straight-line (degree-1) interpolant through two adjacent tabulated points and integrating that line exactly, then to show why summing single panels over [a,b][a,b] collapses interior ordinates to weight 22 in the composite formula.

Step 1. On [xi−1,xi][x_{i-1},x_i] with spacing hh, the straight line through (xi−1,fi−1)(x_{i-1},f_{i-1}) and (xi,fi)(x_i,f_i) is the linear interpolant L(x)=fi−1+fi−fi−1h(x−xi−1)L(x) = f_{i-1} + \dfrac{f_i - f_{i-1}}{h}(x - x_{i-1}). This is exactly the line a ruler would draw between the two tabulated points — no information beyond those two values is used.

Step 2. Integrate L(x)L(x) over the panel: ∫xi−1xiL(x) dx=[fi−1(x−xi−1)+fi−fi−12h(x−xi−1)2]xi−1xi\displaystyle\int_{x_{i-1}}^{x_i} L(x)\,dx = \Big[f_{i-1}(x-x_{i-1}) + \frac{f_i-f_{i-1}}{2h}(x-x_{i-1})^2\Big]_{x_{i-1}}^{x_i}. Substituting the upper limit x−xi−1=hx - x_{i-1} = h gives fi−1h+fi−fi−12hh2=fi−1h+h2(fi−fi−1)f_{i-1}h + \dfrac{f_i-f_{i-1}}{2h}h^2 = f_{i-1}h + \dfrac{h}{2}(f_i - f_{i-1}).

Step 3. Simplify: fi−1h+h2fi−h2fi−1=h2fi−1+h2fi=h2(fi−1+fi)f_{i-1}h + \dfrac{h}{2}f_i - \dfrac{h}{2}f_{i-1} = \dfrac{h}{2}f_{i-1} + \dfrac{h}{2}f_i = \dfrac{h}{2}(f_{i-1}+f_i) — the familiar trapezoid area, base hh and average height 12(fi−1+fi)\tfrac{1}{2}(f_{i-1}+f_i).

Step 4. Sum over all nn panels, i=1i = 1 to nn: ∑i=1nh2(fi−1+fi)=h2[(f0+f1)+(f1+f2)+⋯+(fn−1+fn)]\sum_{i=1}^{n}\dfrac{h}{2}(f_{i-1}+f_i) = \dfrac{h}{2}\Big[(f_0+f_1)+(f_1+f_2)+\cdots+(f_{n-1}+f_n)\Big]. Every interior ordinate f1,…,fn−1f_1,\dots,f_{n-1} appears in exactly two adjacent brackets, so it is counted twice, while f0f_0 and fnf_n each appear once. Collecting terms gives exactly h2[f0+2(f1+⋯+fn−1)+fn]\dfrac{h}{2}\big[f_0 + 2(f_1+\cdots+f_{n-1}) + f_n\big], confirming the composite formula used above.

Worked example

Estimate ∫01.211+x2 dx\displaystyle\int_0^{1.2} \frac{1}{1+x^2}\,dx by both rules using 66 subintervals (h=0.2h=0.2), and compare against the exact value arctan⁡(1.2)≈0.8761\arctan(1.2) \approx 0.8761.

Step 1. Tabulate f(x)=11+x2f(x) = \dfrac{1}{1+x^2} at x=0,0.2,0.4,0.6,0.8,1.0,1.2x = 0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2: f0=1.0000f_0 = 1.0000, f1=0.9615f_1 = 0.9615, f2=0.8621f_2 = 0.8621, f3=0.7353f_3 = 0.7353, f4=0.6098f_4 = 0.6098, f5=0.5000f_5 = 0.5000, f6=0.4098f_6 = 0.4098.

Step 2. Apply the trapezoidal rule. The interior sum is 0.9615+0.8621+0.7353+0.6098+0.5000=3.66870.9615+0.8621+0.7353+0.6098+0.5000 = 3.6687, so

∫01.2f dx≈0.22[(1.0000+0.4098)+2(3.6687)]=0.1[1.4098+7.3374]=0.1(8.7472)=0.87472\int_0^{1.2} f\,dx \approx \frac{0.2}{2}\big[(1.0000+0.4098) + 2(3.6687)\big] = 0.1\big[1.4098 + 7.3374\big] = 0.1(8.7472) = 0.87472

Trapezoidal estimate: ≈0.8747\approx 0.8747, an error of about 0.00140.0014 against the exact 0.87610.8761.

Step 3. Apply Simpson's 1/31/3 rule on the same table (n=6n=6 is even, as required). Odd-indexed ordinates f1,f3,f5f_1,f_3,f_5 sum to 0.9615+0.7353+0.5000=2.19680.9615+0.7353+0.5000 = 2.1968; even-interior ordinates f2,f4f_2,f_4 sum to 0.8621+0.6098=1.47190.8621+0.6098 = 1.4719.

∫01.2f dx≈0.23[(1.0000+0.4098)+4(2.1968)+2(1.4719)]=0.23[1.4098+8.7872+2.9438]=0.23(13.1408)≈0.87605\int_0^{1.2} f\,dx \approx \frac{0.2}{3}\big[(1.0000+0.4098) + 4(2.1968) + 2(1.4719)\big] = \frac{0.2}{3}\big[1.4098+8.7872+2.9438\big] = \frac{0.2}{3}(13.1408) \approx 0.87605

Step 4. Compare. Simpson's estimate 0.87610.8761 matches the exact arctan⁡(1.2)≈0.8761\arctan(1.2) \approx 0.8761 to four decimal places, an error smaller than 0.00010.0001 — roughly ten times more accurate than the trapezoidal estimate on identical data and identical hh. This is the expected pattern: for a smooth integrand, Simpson's parabolic fit tracks curvature that the trapezoidal straight-line fit misses, so it wins decisively even at a coarse hh. Trapezoidal ≈0.8747\approx 0.8747; Simpson's 1/3≈0.87611/3 \approx 0.8761; exact ≈0.8761\approx 0.8761.

Common traps

  • Applying Simpson's 1/31/3 rule with an odd number of subintervals — the formula strictly requires nn even; with odd nn, fall back to the trapezoidal rule (or treat one leftover panel separately, which is outside this course's scope).
  • Getting the weight pattern wrong — it is 1,4,2,4,2,…,4,11,4,2,4,2,\dots,4,1 for Simpson's rule and 1,2,2,…,2,11,2,2,\dots,2,1 for the trapezoidal rule; the first and last weights are always 11, and mixing up which interior ordinates get 44 versus 22 is the single most common exam slip.
  • Mis-copying the scaling factor — h2\dfrac{h}{2} for the trapezoidal rule versus h3\dfrac{h}{3} for Simpson's rule; under exam pressure these are easy to swap, so always sanity-check the factor against the formula before submitting a numeric answer.
  • Assuming a finer hh is always available — for tabulated data hh is fixed by the given table, and refining it is not an option; the skill being tested is choosing and correctly applying the rule that fits the given nn, not adaptive step control (outside this course).

Ask the tutor: If the Simpson's-rule weight pattern keeps tripping you up, ask the AI tutor for a mnemonic or to check your working on a fresh integral.

Why does Simpson's rule need an even number of subintervals but the trapezoidal rule does not?

Simpson's rule builds each parabolic panel from three consecutive nodes spanning two subintervals, so the nn subintervals must pair up exactly — hence nn even. The trapezoidal rule only ever needs two adjacent nodes per panel, so any single subinterval can be handled on its own and nn can be anything from 11 upward. If your data table has an odd number of intervals, you cannot apply Simpson's rule directly to the whole range; the standard fix taught at this level is simply to use the trapezoidal rule instead.

If Simpson's rule is more accurate, why learn the trapezoidal rule at all?

Because it is simpler, has no parity restriction, and is often the only option when the data hands you an odd number of intervals. It is also the natural first step conceptually — Simpson's rule itself is best understood as an upgrade that replaces straight-line panels with parabolic ones. In this course you are expected to recognize which rule the given data supports and apply it correctly, not to always prefer one over the other regardless of the data.

How much accuracy do I actually gain by switching from trapezoidal to Simpson's on the same table?

In the worked example above, using the identical six-point table, the trapezoidal error was about 0.00140.0014 while Simpson's error was under 0.00010.0001 — roughly a tenfold improvement with no extra function evaluations. That gain comes purely from fitting a parabola instead of a line through the same data; it costs nothing extra to compute once you have the table, which is why Simpson's rule is preferred whenever nn is even and the integrand is reasonably smooth.

Before this

Where this leads