Trapezoidal Rule#

For the trapezoidal rule we approximate the integral of \(f(x)\) on the interval \([a-b]\) by constructing the trapezium below:

../../../_images/trapezoidal_4_0.png

and calculating it’s area.

The area of the trapezium is given by:

\[ A_\text{trapezoid} = A_{\text{rectangle}} + A_{\text{triangle}} \]

In the case where \(f(a) < f(b)\), this area is given by:

\[\begin{align*} A_\text{trapezoid} &= (b - a)f(a) + \tfrac{1}{2} (b - a) \left[f(b) - f(a)\right]\\ &= \tfrac{1}{2}(b - a) \left[f(a) + f(b)\right] \end{align*}\]

In the case where \(f(a) > f(b)\), the area is give by:

\[\begin{align*} A_\text{trapezoid} &= (b - a)f(b) + \tfrac{1}{2} (b - a) \left[f(a) - f(b)\right]\\ &= \tfrac{1}{2}(b - a) \left[f(a) + f(b)\right] \end{align*}\]

which is the same as above, so in general we can approximate the integral as:

\[ \int_a^b f(x)~ dx \approx \tfrac{1}{2} (b - a) \left[f(a) + f(b)\right] \]

Composite Trapezoid Rule#

Now, if we were to break up this interval into \(n\) equal sub-intervals, and approximate the integral on each of these, we arrive at the composite trapezoidal rule (illustrated in the diagrams that follow).

../../../_images/trapezoidal_8_0.png ../../../_images/trapezoidal_8_1.png ../../../_images/trapezoidal_8_2.png

To calculate this we use the sum:

\[ \int_a^b f(x)~ dx \approx \sum_{i=1}^n \tfrac{1}{2} (x_i - x_{i-1}) \left[f(x_{i-1}) + f(x_i)\right] \]

where \(x_0 = a\) and \(x_n = b\). As we have specified that each of the \(n\) subintervals are of equal sizes, we have that:

\[ x_{i} - x_{i - 1} = \frac{b- a}{n} \]

we can therefore write the approximation as:

\[ \int_a^b f(x)~ dx \approx \frac{b -a}{2 n} \sum_{i=1}^n \left[f(x_{i-1}) + f(x_i)\right] \]

note how each \(f(x_i)\) in the sum above is repeated twice, except for \(f(x_0)\) and \(f(x_n)\), which only feature once each. We can now write the approximation as:

\[ \int_a^b f(x)~ dx \approx \frac{b -a}{2 n} \left[f(a) + 2 \left\{\sum_{i=1}^{n-1} f(x_i)\right\} + f(b)\right] \]

For a choice of \(n\) such that \(0 < \tfrac{b - a}{n} < 1\), the error for this method is \(O\left(\tfrac{1}{n}^2\right)\) [IntTrap1].

Composite Trapezoidal Rule with a Discrete Data Set#

Again, consider the data set \((x_i, y_i)\) for \(i = 0, \dots, n\), where

\[ f(x_i) = y_i \]

If we wanted to approximate the integral of this data set using the trapezoidal rule, we can apply this to each interval individually:

\[ \int_{x_0}^{x_n} f(x)~ dx \approx \tfrac{1}{2} \sum_{i = 1}^n (x_i - x_{i-1}) \left[y_{i-1} + y_i\right] \]

If the \(x_i\) values are evenly spaced, with \(x_i - x_{i-1} = \Delta x\) constant, then we can use the composite formula from the section above:

\[ \int_{x_0}^{x_n} f(x)~ dx \approx \frac{\Delta x}{2} \left[ y_0 + 2 \left\{\sum_{i=1}^{n-1} y_i\right\} + y_n \right] \]

References#

IntTrap1

James F. Epperson. An Introduction to Numerical Methods and Analysis. John Wiley & Sons, Inc., Hoboken, New Jersey, second edition edition, 2013.