





The trapezoidal rule gives us a technique to approximate the integral on a given interval [a, b], but we cannot reduce the error because the error depends on the width of the interval over which we are integrating.
By dividing the interval [a, b] into many smaller intervals, and applying the trapezoidal rule to each, this allows us to find a better approximation the integral.
Background
Useful background for this topic includes:
- 3. Iteration
- 12.1 Trapezoidal Rule
References
- Bradie, Section 7.1, Introduction to Quadrature, p.355.
- Mathews, Section 7.2, Composite Trapezoidal and Simpson's Rule, p.364.
Theory
Assumptions
We will assume that f(x) is a scalar-valued function of a single variable x and, for error analysis, that f(x) has a continuous 2nd derivative f(2)(x) which we can compute.
Derivation
We cannot reduce the error of the trapezoidal rule by changing (b - a) because this would require us to change the interval over which we are integrating. Therefore, instead, break up the interval [a, b] into n equally sized sub-intervals and apply the trapezoid rule to each of these subintervals.
For example, Figure 1 shows a single application of the trapezoidal rule.

Figure 1. The integral of cos(x) on [0, π/2] approximated with one trapezoid.
Figure 2 shows the trapezoidal rule applied to four sub-intervals on the interval [0, π/2]. It should be quite obvious that the sum of the area of the four trapezoids is a much better approximation to the integral than is the area under a single trapezoid shown in Figure 1.

Figure 2. The integral of cos(x) on [0, π/2] approximated with four trapezoids.
Thus, suppose we divide the interval [a, b] into n sub-intervals, where h = (b - a)/n. Thus, the trapezoidal rule applied to the ith interval is
where i = 1, 2, ..., n. By adding these n areas together we get
Notice that every term except for the first and last is duplicated, and the last term f(a + nh) = f(b). Thus, by factoring out ½ and h, we get the approximation
HOWTO
Problem
Given a function of one variable, f(x), find the area under the curve (the integral) on the interval [a, b].
Assumptions
We will assume the function is Riemann integrable. For error analysis, We will assume that the function f(x) is continuous and has a continuous derivative.
Tools
We will use sampling and iteration.
Initial Requirements
We start with the single interval [a, b], set h = b - a, and let T0 = ½ (f(a) + f(b))h.
Iteration Process
For n = 1, 2, ..., we divide the interval [a, b] into 2n sub-intervals (and therefore h = (b - a)/2n) and we set
Halting Conditions
There are two conditions which may cause the iteration process to halt:
- We halt if the step between successive iterates is sufficiently small, |Tn - Tn − 1| < εstep
- If we have iterated some maximum number of times, say N, and have not met Condition 1, we halt and indicate that a solution was not found.
If we halt due to Condition 1, we state that Tn is our approximation to the integral.
If we halt due to Condition 2, we may state that a solution may not exist.
Examples
Example 1.
Approximate the integral of f(x) = x3 on the interval [1, 2] with four subintervals.
First, h = (2 - 1)/4 = 0.25, and thus we calculate:
= ½⋅(13 + 2⋅(1.253 + 1.53 + 1.753) + 23)⋅0.25 = 3.796875
If we double the number of intervals, that is, eight, we set h = (2 - 1)/8 = 0.125, and thus we calculate:
= ½⋅(13+2⋅(1.1253+1.253+1.3753+1.53+1.6253+1.753+1.8753)+23)⋅0.25 = 3.76171875
The second approximation is much closer to the correct answer of 3.75.
Example 2
Find the integral of the function f(x) = e-xsin(x) on the interval [0, 3], with N = 10 and εstep = 0.001. In this case, the initial width is h = 3.0 and therefore T0 = 0.010539. The subsequent iterations are shown in Table 1.
Table 1. The composite-trapezoidal rule applied to f(x) = e-xsin(x).
n | Tn − 1 | Tn | |Tn - Tn − 1| |
---|---|---|---|
1 | 0.010539 | 0.33913 | 0.33 |
2 | 0.33913 | 0.47256 | 0.13 |
3 | 0.47256 | 0.50881 | 0.036 |
4 | 0.50881 | 0.51804 | 0.0092 |
5 | 0.51804 | 0.52036 | 0.0023 |
6 | 0.52036 | 0.52094 | 0.00058 |
Thus, our approximation to the integral is 0.52094 which has an absolute error of 0.00019 (or relative error of less than 0.04%) when compared to the correct five digit approximation 0.52113.
Engineering
To be done later.
Error
The error for the trapezoidal rule applied to a single interval [a, b] is given by
where ξ is some value in the interval [a, b], that is, the interval of integration.
If we are applying the composite trapezoidal rule to n intervals, each of width h = (b - a)/n, the error for the composite-trapezoidal rule is the sum of the errors on each of the individual intervals, namely:
where ξi ∈ [a + (i - 1)h, a + ih].
To simplify this expression, we note that the sum approximates n times the average value of f(2)(x)
and thus, by substituting this into the original equation, we have that the error may be approximated by:
Example
To demonstrate this, suppose we integrate f(x) = x e-x on the interval [0, 4] with 10 intervals. This gives us an approximation of 0.8944624935 whereas the correct answer (to 10 decimal digits) is 0.9084218056, and therefore the difference (the error) is 0.0139593121.
The average value of the second derivative of f(x) is -0.2637367292, and if we multiply this by -1/12 ⋅ 4 ⋅ 0.42, we get that a good approximation of the error should be 0.01406595889, which is very close to the actual error of our approximation.
Questions
Question 1
Approximate the integral of f(x) = e-x on [0, 10] with 20 subintervals.
Answer: 1.02070069942442
Question 2
Approximate the integral of f(x) = e-x on [0, 10] starting with one interval and continuing until the difference between two approximations (εstep) is less than 0.000001.
Answer: after 13 iterations, we get 0.999954724240937
Question 3
Approximate the integral of f(x) = x2 on the interval [-2, 2] with 4 and 8 intervals.
Answer: 6 and 5.5
Question 4
What is the estimated error (using the mean of the 2nd derivative) for the approximate in Question 3 when using eight intervals and what is the actual error?
Answer: the estimated and actual errors are equal -1/6 because the second derivative of x2 is 2.
Question 5
Approximate the integral of f(x) = x4 on the interval [-2, 2] with 4 and 8 intervals.
Answer: 18 and 14.125
Question 6
What is the estimated error for the approximate in Question 5 when using eight intervals and what is the actual error?
Answer: the estimated of the error is -1.3333333 whereas the actual error is -1.325⋅⋅⋅.
Matlab
Finding approximate the integral using the composite trapezoidal rule, of a function f(x) = cos(x):
format long eps_step = 1e-5; N = 20; a = 0; b = 10; h = b - a; ctrap = 0.5*(cos(a) + cos(b))*h; for i=1:N h = h/2; ctrapn = 0.5*(cos(a) + 2*sum( cos( (a + h):h:(b - h) ) ) + cos(b))*h; if abs( ctrap - ctrapn ) < eps_step break; elseif i == N error( 'The composite trapezoidal rule did not converge' ); end ctrap = ctrapn; end ctrapn
What happens if you remove the conditional statement and simply approximate the integral with larger and larger numbers of sub-intervals? (Try N = 100;)
Maple
The following commands in Maple:
with( Student[Calculus1] ): ApproximateInt( 2*cos(4*x)*exp(-x), x = 0.0..5.0, method = trapezoid, output = animation, partition = 1, iterations = 10 );
produces the animation shown in Figure 1 (click to enlarge).

Figure 1. The composite-trapezoidal rule applied to 2 cos(4 x) e-x with 1, 2, 4, ..., 512 intervals.
For more help on approximating integrals or on the Student[Calculus1] package, enter:
?ApproximateInt ?Student,Calculus1
Copyright ©2005 by Douglas Wilhelm Harder. All rights reserved.