Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

 Open In Colab

Derivation of the Trapezoid Rule

The next polynomial degree is 1; a line.

Given the points xix_i, xi+1x_{i+1}, fif_i, and fi+1f_{i+1}:

Trapezoidal rule illustration
f(x)≈fi+fi+1−fixi+1−xi(x−xi)f(x) \approx f_i + \frac{f_{i+1} - f_i}{x_{i+1} - x_i}(x - x_i)

Integrating this linear interpolant over [xi,xi+1][x_i, x_{i+1}] with step h=xi+1−xih = x_{i+1} - x_i:

∫xixi+1f(x) dx≈h2[fi+fi+1]\int_{x_i}^{x_{i+1}} f(x) \,\mathrm{d}x \approx \frac{h}{2} [f_i + f_{i+1}]

Summing over all nn subdivisions yields the composite trapezoid rule:

∫abf(x) dx≈h2[f(a)+2∑i=1n−1f(xi)+f(b)]\int_a^b f(x) \,\mathrm{d}x \approx \frac{h}{2} \left[f(a) + 2 \sum_{i=1}^{n-1} f(x_i) + f(b)\right]
numerical_integration_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def trapezoid_rule(f, a, b, n):
    h = (b - a) / n
    x_points = np.linspace(a, b, n + 1)
    y_points = f(x_points)

    widths = np.diff(x_points)
    integral_approx = np.sum(widths * (y_points[:-1] + y_points[1:]) / 2)

    return {
        "x_points": x_points,
        "y_points": y_points,
        "widths": widths,
        "n": n,
        "integral_approx": integral_approx,
        "a": a,
        "b": b,
        "h": h,
    }

Loading...
Loading...

Error Analysis of the Trapezoid Rule

The accuracy is calculated from the Taylor series expansion of f(x)f(x) around the midpoint yi=xi+1+xi2y_i = \frac{x_{i+1} + x_i}{2}:

f(x)=f(yi)+f′(yi)(x−yi)+f′′(yi)2(x−yi)2+O(h3)∫xixi+1f(x) dx=f(yi)h+f′′(yi)24h3+O(h5)\begin{aligned} f(x) &= f(y_i) + f'(y_i)(x - y_i) + \frac{f''(y_i)}{2}(x - y_i)^2 + O(h^3) \\ \int_{x_i}^{x_{i+1}} f(x) \,\mathrm{d}x &= f(y_i) h + \frac{f''(y_i)}{24} h^3 + O(h^5) \end{aligned}

Meanwhile, evaluating the trapezoid approximation with the same expansion gives:

h2[f(xi)+f(xi+1)]=f(yi)h+f′′(yi)8h3+O(h5)\frac{h}{2}[f(x_i) + f(x_{i+1})] = f(y_i) h + \frac{f''(y_i)}{8} h^3 + O(h^5)

The difference yields the local truncation error:

Elocal=−f′′(yi)12h3E_{local} = -\frac{f''(y_i)}{12} h^3

Summing over all n=(b−a)/hn = (b-a)/h elements gives a composite error of O(h2)O(h^2), confirming second-order convergence!

Example: Comparing Trapezoid and Midpoint Methods

Integrate sin⁡(x)\sin(x) from 0 to π\pi (exact analytical value = 2) with the midpoint and trapezoid rules across increasing resolutions:

n =  2: Trapezoid error = 0.429204, Midpoint error = 0.221441
n =  4: Trapezoid error = 0.103881, Midpoint error = 0.052344
n =  8: Trapezoid error = 0.025768, Midpoint error = 0.012909
n = 16: Trapezoid error = 0.006430, Midpoint error = 0.003216
n = 32: Trapezoid error = 0.001607, Midpoint error = 0.000803

Observations: Midpoint vs. Trapezoid

Notice we get quadratic convergence (O(h2)O(h^2)) for both methods, but the midpoint method is actually twice as accurate for the same number of intervals!

However, the midpoint rule requires evaluation at interior points yiy_i, which are not available when working with fixed discrete datasets. The trapezoid rule, by contrast, operates directly on endpoint samples!