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.

9.3 Trapezoidal Rule

#%config InlineBackend.figure_format = 'svg'
from pylab import *

9.3.1Single interval

Consider a function f:[a,b]→Rf : [a,b] \to \re and let us approximate this by a polynomial of degree one, say p1(x)p_1(x). This can be obtained by interpolation

p1(a)=f(a),p1(b)=f(b)p_1(a) = f(a), \qquad p_1(b) = f(b)

Then the approximate integral is

I1(f)=∫abp1(x)dx=b−a2[f(a)+f(b)]I_1(f) = \int_a^b p_1(x) \ud x = \frac{b-a}{2}[f(a) + f(b)]
<Figure size 640x480 with 1 Axes>

Note that this is the area under the straight line curve p1(x)p_1(x) which has the shape of a (rotated) trapezoid. To study the error in this approximation, we start with the error in interpolation (30)

f(x)−p1(x)=(x−a)(x−b)f[a,b,x]f(x) - p_1(x) = (x-a)(x-b) f[a,b,x]

Then, the error in the integral is

E1(f)=∫ab[f(x)−p1(x)]dx=∫ab(x−a)(x−b)f[a,b,x]dx\begin{aligned} E_1(f) &= \int_a^b [f(x) - p_1(x)] \ud x \\ &= \int_a^b (x-a) (x-b) f[a,b,x] \ud x \end{aligned}

Using integral mean value theorem

E1(f)=f[a,b,ξ]∫ab(x−a)(x−b)dx,for some ξ∈[a,b]E_1(f) = f[a,b,\xi] \int_a^b (x-a)(x-b) \ud x, \qquad \textrm{for some $\xi \in [a,b]$}

Finally, using the properties of divided differences (65)

E1(f)=[12f′′(η)][−16(b−a)3],for some η∈[a,b]=−112(b−a)3f′′(η)\begin{aligned} E_1(f) &= \left[ \half f''(\eta) \right] \left[ -\frac{1}{6} (b-a)^3 \right], \qquad \textrm{for some $\eta \in [a,b]$} \\ &= -\frac{1}{12} (b-a)^3 f''(\eta) \end{aligned}

The error will be small only if b−ab-a is small.

9.3.2Composite trapezoidal rule

Divide [a,b][a,b] into nn equal intervals by the partition

a=x0<x1<…<xn=bwith spacingh=b−ana = x_0 < x_1 < \ldots < x_n = b \qquad \textrm{with spacing} \qquad h = \frac{b-a}{n}

and

xj=a+jh,j=0,1,2,…,nx_j = a + j h, \qquad j=0,1,2,\ldots, n
<Figure size 800x500 with 1 Axes>

Let us use the Trapezoidal rule in each interval [xj−1,xj][x_{j-1},x_j]

I(f)=∫abf(x)dx=∑j=1n∫xj−1xjf(x)dxI(f) = \int_a^b f(x) \ud x = \sum_{j=1}^n \int_{x_{j-1}}^{x_j} f(x) \ud x

From the previous section, we know that

∫xj−1xjf(x)dx=h2[f(xj−1)+f(xj)]−h312f′′(ηj),ηj∈[xj−1,xj]\int_{x_{j-1}}^{x_j} f(x) \ud x = \frac{h}{2}[f(x_{j-1}) + f(x_j)] - \frac{h^3}{12}f''(\eta_j), \qquad \eta_j \in [x_{j-1}, x_j]

Hence

I(f)=∑j=1nh2[f(xj−1)+f(xj)]−∑j=1nh312f′′(ηj)=In(f)−∑j=1nh312f′′(ηj)\begin{aligned} I(f) &= \clr{red}{ \sum_{j=1}^n \frac{h}{2}[f(x_{j-1}) + f(x_j)] } - \sum_{j=1}^n \frac{h^3}{12}f''(\eta_j) \\ &= \clr{red}{ I_n(f) } - \sum_{j=1}^n \frac{h^3}{12}f''(\eta_j) \end{aligned}

where

In(f)=h[12f0+f1+f2+…+fn−1+12fn]I_n(f) = h [ \shalf f_0 + f_1 + f_2 + \ldots + f_{n-1} + \shalf f_n]

is the composite Trapezoidal rule. The error is

En(f)=I(f)−In(f)=−h312∑j=1nf′′(ηj)=−nh3121n∑j=1nf′′(ηj)⏟MnE_n(f) = I(f) - I_n(f) = - \frac{h^3}{12} \sum_{j=1}^n f''(\eta_j) = - \frac{n h^3}{12} \underbrace{\frac{1}{n} \sum_{j=1}^n f''(\eta_j)}_{M_n}

where

min⁡x∈[a,b]f′′(x)≤min⁡1≤j≤nf′′(ηj)≤Mn≤max⁡1≤j≤nf′′(ηj)≤max⁡x∈[a,b]f′′(x)\min_{x \in [a,b]} f''(x) \le \min_{1 \le j \le n} f''(\eta_j) \le M_n \le \max_{1 \le j \le n} f''(\eta_j) \le \max_{x \in [a,b]} f''(x)

If f′′(x)f''(x) is continuous, then it attains all values between the minimum and maximum.

  ⟹  ∃η∈[a,b]such thatMn=f′′(η)\implies \exists \eta \in [a,b] \qquad \textrm{such that} \qquad M_n = f''(\eta)

Hence, after using nh=b−anh=b-a, the error is given by

En(f)=−112(b−a)h2f′′(η),for some η∈[a,b]E_n(f) = - \frac{1}{12}(b-a) h^2 f''(\eta), \qquad \textrm{for some $\eta \in [a,b]$}

The error goes to zero as n→∞n \to \infty since h→0h \to 0.

9.3.3Asymptotic error estimate

We can estimate the error as

∣En(f)∣≤112(b−a)h2max⁡x∈[a,b]∣f′′(x)∣|E_n(f)| \le \frac{1}{12}(b-a) h^2 \max_{x \in [a,b]}|f''(x)|

provided we can estimate the second derivative. A more useful error estimate can be derived as follows.

Using the error of the composite rule, we can compute the limit

lim⁡n→∞En(f)h2=−112lim⁡n→∞h∑j=1nf′′(ηj),ηj∈[xj−1,xj]=−112∫abf′′(x)dx=−112[f′(b)−f′(a)]\begin{aligned} \lim_{n \to \infty} \frac{E_n(f)}{h^2} &= - \frac{1}{12} \lim_{n \to \infty} h \sum_{j=1}^n f''(\eta_j), \qquad \eta_j \in [x_{j-1}, x_j] \\ &= -\frac{1}{12} \int_a^b f''(x) \ud x \\ &= -\frac{1}{12}[f'(b) - f'(a)] \end{aligned}

Hence for large nn

En(f)≈−h212[f′(b)−f′(a)]=:E~n(f)E_n(f) \approx -\frac{h^2}{12}[f'(b) - f'(a)] =: \tilde E_n(f)

9.3.4Corrected trapezoidal rule

Using the asymptotic error estimate

I(f)−In(f)≈E~n(f)=−h212[f′(b)−f′(a)]  ⟹  I(f)≈In(f)−h212[f′(b)−f′(a)]\begin{gather} I(f) - I_n(f) \approx \tilde E_n(f) = -\frac{h^2}{12}[f'(b) - f'(a)] \\ \implies I(f) \approx I_n(f) -\frac{h^2}{12}[f'(b) - f'(a)] \end{gather}

we can define a corrected Trapezoidal rule as

Cn(f)=In(f)−h212[f′(b)−f′(a)]=h[12f0+f1+f2+…+fn−1+12fn]−h212[f′(b)−f′(a)]\begin{align} C_n(f) &= I_n(f) -\frac{h^2}{12}[f'(b) - f'(a)] \\ &= h \left[ \shalf f_0 + f_1 + f_2 + \ldots + f_{n-1} + \shalf f_n \right] - \frac{h^2}{12}[f'(b) - f'(a)] \end{align}