## Numerical Integration: Trapezoidal Rule

### Trapezoidal Rule

Let . By dividing the interval into many subintervals, the trapezoidal rule approximates the area under the curve by linearly interpolating between the values of the function at the junctions of the subintervals, and thus, on each subinterval, the area to be calculated has a shape of a trapezoid. For simplicity, the width of the trapezoids is chosen to be constant. Let be the number of intervals with and constant spacing . The trapezoidal method can be implemented as follows:

The following tool illustrates the implementation of the trapezoidal rule for integrating the function defined as:

Use the slider to see the effect of increasing the number of intervals on the approximation.

The following Mathematica code can be used to numerically integrate any function on the interval using the trapezoidal rule.

View Mathematica Code
IT[f_, a_, b_, n_] := (h = (b - a)/n; Sum[(f[a + (i - 1)*h] + f[a + i*h])*h/2, {i, 1, n}])
f[x_] := 2 + 2 x + x^2 + Sin[2 Pi*x] + Cos[2 Pi*x/0.5];
IT[f, 0, 1.5, 1.0]
IT[f, 0, 1.5, 18.0]

View Python Code
import numpy as np
def IT(f, a, b, n):
h = (b - a)/n
return sum([(f(a + i*h) + f(a + (i + 1)*h))*h/2 for i in range(int(n))])
def f(x): return 2 + 2*x + x**2 + np.sin(2*np.pi*x) + np.cos(2*np.pi*x/0.5)
print(IT(f, 0, 1.5, 1.0))
print(IT(f, 0, 1.5, 18.0))


The following link provides the MATLAB codes for implementing the trapezoidal rule.

MATLAB files:

#### Error Analysis

An extension of Taylor’s theorem can be used to find how the error changes as the step size decreases. Given a function and its interpolating polynomial of degree (), the error term between the interpolating polynomial and the function is given by (See the Mathematical Background section for a proof):

Where is in the domain of the function and is dependent on the point . The error in the calculation of trapezoidal number between the points and will be estimated based on the above formula assuming a linear interpolation between the points and . Therefore:

If is the number of subdivisions (number of trapezoids), i.e., , then:

In the above error analysis, we disregarded higher order terms than . Although this simplified the calculations and sufficed our purpose, here we demonstrate a general error analysis containing higher order terms. We will use the results later in explaining the Romberg’s Method.

Consider a continuous function defined on an interval . If the interval is discretized into sub intervals such that , the trapezoidal rule estimates the integration of over a sub interval as:

where

To find the order of error of the above estimation, we should find the terms represented by in:

(I)

where in the order of accuracy (to be determined).

To this end, we first consider writing for by its the Taylor series expansion about being is the midpoint of the interval . Therefore, substituting the Taylor series expansion into leads to,

which is eventually evaluated as,

(II)

Leaving this result for now and getting back to the trapezoidal rule, we can write using its Taylor series expansion as follows.

If the Taylor series expansions for and are written as,

therefore,

Note that the derivatives of odd order, i.e. vanish.

Solving the above equation for and substituting the resultant expression into the right-hand side of Eq. II, leads to,

Comparing this result with Eq. I, we can say that the trapezoidal rule is third-order accurate over a sub-interval, in other words,

(I)

We should now proceed to analyze the error of the trapezoidal rule over the entire interval . If we consider a similar length, , for each sub interval of discretized as , we can write,

The summations of terms of derivatives can be replaced with their values determined using the intermediate value theorem. The intermediate value theorem states that if a function is continuous over the interval , there is such that for any falling within the values of and .

Therefore, for the continuous function over , if we set and within such that and , we can write,

meaning that there is a number such that . Consequently,

With the same method, we can write as,

for .

Using the above values of the summations we get,

As , we can conclude that

Indicating that the trapezoidal rule for integration of over any interval is second-order accurate.

#### Example

Using the trapezoidal rule with , calculate and compare with the exact integral of the function on the interval . Find the value of so that the error is less than 0.001.

##### Solution

Since the spacing can be calculated as:

Therefore, , , , , and . The values of the function at these points are given by:

According to the trapezoidal rule we have:

Which is already a very good approximation to the exact value of even though only 4 intervals were used.

##### Error Bounds

The total error obtained when is indeed less than the error bounds obtained by the formula listed above. For , the error in the estimation is bounded by:

The error is indeed less than that upper bound. The same formula can be used to find the value of so that the error is bounded by 0.001:

Therefore, to guarantee an error less than 0.001 using , the interval will have to be divided into: parts. Therefore, at least 71 trapezoids are needed! In this case, and .