You are on page 1of 8

2.

5: Numerical Integration - Midpoint, Trapezoid, Simpson's rule


The antiderivatives of many functions either cannot be expressed or cannot be expressed easily in closed form (that is, in terms of known functions).
Consequently, rather than evaluate definite integrals of these functions directly, we resort to various techniques of numerical integration to approximate
their values. In this section we explore several of these techniques. In addition, we examine the process of estimating the error in using these techniques.
The Midpoint Rule
Earlier in this text we defined the definite integral of a function over an interval as the limit of Riemann sums. In general, any Riemann sum of a
b

function f(x) over an interval [a, b] may be viewed as an estimate of ∫ f(x) dx . Recall that a Riemann sum of a function f(x) over an interval [a, b]
a

is obtained by selecting a partition

P = {x0 , x1 , x2 , … , xn }, where a = x0 < x1 < x2 < ⋯ < xn = b

and a set
∗ ∗ ∗ ∗
S = {x , x , … , xn }, where xi−1 ≤ x ≤ xi for all i.
1 2 i

The Riemann sum corresponding to the partition P and the set S is given by n ∑ f(x ∗
i
)Δxi , where Δx i = xi − xi−1 , the length of the i th
subinterval.
i=1

The midpoint rule for estimating a definite integral uses a Riemann sum with subintervals of equal width and the midpoints, mi , of each subinterval in
place of x . Formally, we state a theorem regarding the convergence of the midpoint rule as follows.

i

The Midpoint Rule


b−a
Assume that f(x) is continuous on [a, b] . Let n be a positive integer and Δx = . If [a, b] is divided into n subintervals, each of length Δx ,
n

and m is the midpoint of the i


i
th
subinterval, set
n

Mn = ∑ f(mi )Δx. (2.5.1)

i=1

Then lim Mn = ∫ f(x) dx.


n→∞
a

As we can see in Figure, if f(x) ≥ 0 over [a, b] , then ∑ f(mi )Δx corresponds to the sum of the areas of rectangles approximating the area between
i=1

the graph of f(x) and the x -axis over [a, b] . The graph shows the rectangles corresponding to M4 for a nonnegative function over a closed interval
[a, b].

Figure 2.5.1 : The midpoint rule approximates the area between the graph of f (x) and the x -axis by summing the areas of rectangles with midpoints that are points on f (x).

Example 2.5.1 : Using the Midpoint Rule with M 4

Use the midpoint rule to estimate ∫ x


2
dx using four subintervals. Compare the result with the actual value of this integral.
0

1−0 1
Solution: Each subinterval has length Δx = = . Therefore, the subintervals consist of
4 4

1 1 1 1 3 3
[0, ], [ , ], [ , ] , and [ , 1] .
4 4 2 2 4 4

The midpoints of these subintervals are { 1

8
,
3

8
,
5

8
,
7

8
}. Thus,
1 1 1 3 1 5 1 7 1 1 1 9 1 25 1 21 21
M4 = ⋅ f( ) + ⋅ f( ) + ⋅ f( ) + ⋅ f( ) = ⋅ + ⋅ + ⋅ + ⋅ = .
4 8 4 8 4 8 4 8 4 64 4 64 4 64 4 64 64

Since
1
2
1
∫ x dx = ,
0
3

∣1 21 ∣ 1
the error in this approximation is: ∣ − ∣ = ≈ 0.0052,
∣3 64 ∣ 192

12/21/2020 2.5.1 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269


and we see that the midpoint rule produces an estimate that is somewhat close to the actual value of the definite integral.

Example 2.5.2 : Using the Midpoint Rule with M 6

Use M to estimate the length of the curve y =


6
1

2
x
2
on [1, 4] .
Solution: The length of y = 1

2
x
2
on [1, 4] is
−−−−−−−−−
4 2
dy
s = ∫ √1 + ( ) dx.
1
dx

4
dy −−−− −
Since = x , this integral becomes ∫ 2
√1 + x dx.
dx 1

4−1 1
If [1, 4] is divided into six subintervals, then each subinterval has length Δx = = and the midpoints of the subintervals are
6 2
−−−−−
. If we set f(x) = √1 + x ,
5 7 9 11 13 15
2
{ , , , , , }
4 4 4 4 4 4

1 5 1 7 1 9 1 11 1 13 1 15
M6 = ⋅ f( ) + ⋅ f( ) + ⋅ f( ) + ⋅ f( ) + ⋅ f( ) + ⋅ f( )
2 4 2 4 2 4 2 4 2 4 2 4


1

2
(1.6008 + 2.0156 + 2.4622 + 2.9262 + 3.4004 + 3.8810) = 8.1431 units.

Exercise 2.5.1
2
1
Use the midpoint rule with n = 2 to estimate ∫ dx.
1
x

Hint
1 5 7
Δx = , m1 = , and m2 = .
2 4 4

Answer
24

35

The Trapezoidal Rule


We can also approximate the value of a definite integral by using trapezoids rather than rectangles. In Figure 2.5.2 , the area beneath the curve is
approximated by trapezoids rather than by rectangles.

Figure 2.5.2 : Trapezoids may be used to approximate the area under a curve, hence approximating the definite integral.

The trapezoidal rule for estimating definite integrals uses trapezoids rather than rectangles to approximate the area under a curve. To gain insight into
the final form of the rule, consider the trapezoids shown in Figure 2.5.2. We assume that the length of each subinterval is given by Δx . First, recall that
the area of a trapezoid with a height of h and bases of length b and b is given by Area = h(b + b ) . We see that the first trapezoid has a height Δx 1 2
1

2
1 2

and parallel bases of length f(x ) and f(x ). Thus, the area of the first trapezoid in Figure 2.5.2 is
0 1

1
Δx(f(x0 ) + f(x1 )).
2

The areas of the remaining three trapezoids are


1 1 1
Δx(f(x1 ) + f(x2 )), Δx(f(x2 ) + f(x3 )), and Δx(f(x3 ) + f(x4 )).
2 2 2

Consequently,
b
1 1 1 1
∫ f(x) dx ≈ Δx(f(x0 ) + f(x1 )) + Δx(f(x1 ) + f(x2 )) + Δx(f(x2 ) + f(x3 )) + Δx(f(x3 ) + f(x4 )).
a
2 2 2 2

After taking out a common factor of 1

2
Δx and combining like terms, we have
b
Δx
∫ f(x) dx ≈ (f(x0 ) + 2 f(x1 ) + 2 f(x2 ) + 2 f(x3 ) + f(x4 )).
a
2

Generalizing, we formally state the following rule.

12/21/2020 2.5.2 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269


The Trapezoidal Rule
b−a
Assume that f(x) is continuous over [a, b] . Let n be a positive integer and Δx = . Let [a, b] be divided into n subintervals, each of length
n

Δx , with endpoints at P = {x0 , x1 , x2 … , xn }.

Set
Δx
Tn = (f(x0 ) + 2 f(x1 ) + 2 f(x2 ) + ⋯ + 2 f(xn−1 ) + f(xn )). (2.5.2)
2

Then, lim Tn = ∫ f(x) dx.


n→+∞
a

Before continuing, let’s make a few observations about the trapezoidal rule. First of all, it is useful to note that
n n
1
Tn = (Ln + Rn ) where L n = ∑ f(xi−1 )Δx and R n = ∑ f(xi )Δx.
2
i=1 i=1

That is, L and R approximate the integral using the left-hand and right-hand endpoints of each subinterval, respectively. In addition, a careful
n n

examination of Figure 2.5.3 leads us to make the following observations about using the trapezoidal rules and midpoint rules to estimate the definite
integral of a nonnegative function. The trapezoidal rule tends to overestimate the value of a definite integral systematically over intervals where the
function is concave up and to underestimate the value of a definite integral systematically over intervals where the function is concave down. On the
other hand, the midpoint rule tends to average out these errors somewhat by partially overestimating and partially underestimating the value of the
definite integral over these same types of intervals. This leads us to hypothesize that, in general, the midpoint rule tends to be more accurate than the
trapezoidal rule.

Figure 2.5.3 :The trapezoidal rule tends to be less accurate than the midpoint rule.

Example 2.5.3 : Using the Trapezoidal Rule


1

Use the trapezoidal rule to estimate ∫ x


2
dx using four subintervals.
0

Solution
The endpoints of the subintervals consist of elements of the set P and Δx = Thus,
1 1 3 1−0 1
= {0, , , , 1} = .
4 2 4 4 4

1
2
1 1 1 1 3
∫ x dx ≈ ⋅ (f(0) + 2 f ( ) +2f( ) +2f( ) + f(1))
4 2 4
0
2 4

1 1 1 9
= (0 + 2 ⋅ +2⋅ +2⋅ + 1)
8 16 4 16

11
=
32

Exercise 2.5.2
2
1
Use the trapezoidal rule with n = 2 to estimate ∫ dx.
1
x

Hint
1
Set Δx = . The endpoints of the subintervals are the elements of the set P = {1,
3

2
, 2} .
2

Answer
17

24

Absolute and Relative Error


An important aspect of using these numerical approximation rules consists of calculating the error in using them for estimating the value of a definite
integral. We first need to define absolute error and relative error.

Definition: absolute and relative error

12/21/2020 2.5.3 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269


If B is our estimate of some quantity having an actual value of A, then the absolute error is given by |A − B| .
The relative error is the error as a percentage of the actual value and is given by
∣ A−B ∣
∣ ∣ ⋅ 100%. (2.5.3)
∣ A ∣

Example 2.5.4 : Calculating Error in the Midpoint Rule


1

Calculate the absolute and relative error in the estimate of ∫ x


2
dx using the midpoint rule, found in Example 2.5.1.
0

1
1
Solution: The calculated value is ∫
2
x dx = and our estimate from the example is M4 =
21

64
. Thus, the absolute error is given by
0
3
1 21 1
∣ − ∣ = ≈ 0.0052.
∣ 3 64
∣ 192

The relative error is


1/192 1
= ≈ 0.015625 ≈ 1.6%.
1/3 64

Example 2.5.5 : Calculating Error in the Trapezoidal Rule


1

Calculate the absolute and relative error in the estimate of ∫ x


2
dx using the trapezoidal rule, found in Example 2.5.3.
0

1
1
Solution: The calculated value is ∫ x
2
dx = and our estimate from the example is T4 =
11

32
. Thus, the absolute error is given by
0
3
1 11 1
∣ − ∣ = ≈ 0.0104.
∣ 3 32
∣ 96

The relative error is given by


1/96
= 0.03125 ≈ 3.1%.
1/3

Exercise 2.5.3
2
1
In an earlier checkpoint, we estimated ∫ dx to be 24

35
using T . The actual value of this integral is ln 2 . Using
2
24

35
≈ 0.6857 and ln 2 ≈ 0.6931,
1
x

calculate the absolute error and the relative error.

Hint
Use the previous examples as a guide.

Answer
absolute error ≈ 0.0074, and relative error ≈ 1.1%

Error Bounds on the Midpoint and Trapezoidal Rules


In the two previous examples, we were able to compare our estimate of an integral with the actual value of the integral; however, we do not typically have
this luxury. In general, if we are approximating an integral, we are doing so because we cannot compute the exact value of the integral itself easily.
Therefore, it is often helpful to be able to determine an upper bound for the error in an approximation of an integral. The following theorem provides
error bounds for the midpoint and trapezoidal rules. The theorem is stated without proof.

Error Bounds for the Midpoint and Trapezoidal Rules


Let f(x) be a continuous function over [a, b] , having a second derivative f ′′
(x) over this interval. If M is the maximum value of |f ′′
(x)| over [a, b] ,
b

then the upper bounds for the error in using M and T to estimate ∫
n n f(x) dx are
a

3
M (b − a)
Error in Mn ≤ (2.5.4)
2
24n

and
3
M (b − a)
Error in Tn ≤ (2.5.5)
2
12n

We can use these bounds to determine the value of n necessary to guarantee that the error in an estimate is less than a specified value.

12/21/2020 2.5.4 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269


Example 2.5.6 : Determining the Number of Intervals to Use
1
2

What value of n should be used to guarantee that an estimate of ∫ e


x
dx is accurate to within 0.01 if we use the midpoint rule?
0

Solution
2 2

We begin by determining the value of M , the maximum value of |f ′′


(x)| over [0, 1] for f(x) = e . Since f'(x) = 2x e x x
, we have
2 2
′′ x 2 x
f (x) = 2e + 4x e .

Thus,
2
′′ x 2
|f (x)| = 2e (1 + 2x ) ≤ 2 ⋅ e ⋅ 3 = 6e.

From the error-bound Equation, we have


3 3
M (b − a) 6e(1 − 0) 6e
Error in Mn ≤ ≤ = .
2 2 2
24n 24n 24n

Now we solve the following inequality for n :


6e
≤ 0.01.
2
24n


−−−
Thus, n ≥ √ 600e

24
≈ 8.24. Since n must be an integer satisfying this inequality, a choice of n = 9 would guarantee that

1
∣ 2

x
∣∫ e dx − Mn ∣ < 0.01.
∣ 0 ∣

Analysis
We might have been tempted to round 8.24 down and choose n = 8 , but this would be incorrect because we must have an integer greater than or
equal to 8.24. We need to keep in mind that the error estimates provide an upper bound only for the error. The actual estimate may, in fact, be a much
better approximation than is indicated by the error bound.

Exercise 2.5.4
1

Use Equation to find an upper bound for the error in using M to estimate ∫ 4
2
x dx.
0

Hint
f
′′
(x) = 2, so M = 2.

Answer
1

192

Simpson’s Rule
With the midpoint rule, we estimated areas of regions under curves by using rectangles. In a sense, we approximated the curve with piecewise constant
functions. With the trapezoidal rule, we approximated the curve by using piecewise linear functions. What if we were, instead, to approximate a curve
using piecewise quadratic functions? With Simpson’s rule, we do just this. We partition the interval into an even number of subintervals, each of equal
x2 x2

width. Over the first pair of subintervals we approximate ∫ f(x) dx with ∫ p(x) dx , where p(x) = Ax
2
+ Bx + C is the quadratic function
x0 x0
x4

passing through (x0 , f(x0 )), (x1 , f(x1 )), and (x2 , f(x2 )) (Figure 2.5.4 ). Over the next pair of subintervals we approximate ∫ f(x) dx with the
x2

integral of another quadratic function passing through (x 2


, f(x2 )), (x3 , f(x3 )), and (x 4
, f(x4 )). This process is continued with each successive pair of
subintervals.

Figure 2.5.4 : With Simpson’s rule, we approximate a definite integral by integrating a piecewise quadratic function.

To understand the formula that we obtain for Simpson’s rule, we begin by deriving a formula for this approximation over the first two subintervals. As we
go through the derivation, we need to keep in mind the following relationships:

12/21/2020 2.5.5 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269


2
f(x0 ) = p(x0 ) = Ax + B x0 + C (2.5.6)
0

2
f(x1 ) = p(x1 ) = Ax + B x1 + C (2.5.7)
1

2
f(x2 ) = p(x2 ) = Ax + B x2 + C (2.5.8)
2

x2 − x0 = 2Δx , where Δx is the length of a subinterval.


(x2 + x0 )
x2 + x0 = 2x1 , since x 1 = .
2

Thus,
x2 x2

∫ f(x) dx ≈ ∫ p(x) dx
x0 x0

x2

2
= ∫ (Ax + Bx + C )dx
x0

x2
A 3
B 2 ∣
= ( x + x + C x) ∣ Find the antiderivative.
3 2 ∣x
0

A B
3 3 2 2
= (x −x )+ (x − x ) + C (x2 − x0 ) Evaluate the antiderivative.
2 0 2 0
3 2

A B
2 2
= (x2 − x0 )(x + x2 x0 + x ) + (x2 − x0 )(x2 + x0 ) + C (x2 − x0 )
2 0
3 2

x2 − x0 x2 − x0
2 2
= (2A(x + x2 x0 + x ) + 3B(x2 + x0 ) + 6C ) Factor out .
2 0
6 6

Δx
2 2 2 2
= ((Ax + B x2 + C ) + (Ax + B x0 + C ) + A(x + 2x2 x0 + x ) + 2B(x2 + x0 ) + 4C ) Rearrange the terms. Note: Δx
2 0 2 0
3

Δx 2
= (f(x2 ) + f(x0 ) + A(x2 + x0 ) + 2B(x2 + x0 ) + 4C ) Factor and substitute:
3

2
f(x2 ) = Ax + B x2 + C and
2

Δx
2
= (f(x2 ) + f(x0 ) + A(2x1 ) + 2B(2x1 ) + 4C ) Substitute x2 + x0 = 2x1 .
3

x2 + x0
Note: x1 = , the mid
2

Δx
= (f(x2 ) + 4f(x1 ) + f(x0 )). Expand and substitute f(x1 ) =
3

x4

If we approximate ∫ f(x) dx using the same method, we see that we have


x2

4
x
Δx
∫ f(x) dx ≈ (f(x4 ) + 4 f(x3 ) + f(x2 )).
x0
3

Combining these two approximations, we get


x4
Δx
∫ f(x) dx = (f(x0 ) + 4 f(x1 ) + 2 f(x2 ) + 4 f(x3 ) + f(x4 )).
x0
3

The pattern continues as we add pairs of subintervals to our approximation. The general rule may be stated as follows.

Simpson’s Rule
b−a
Assume that f(x) is continuous over [a, b] . Let n be a positive even integer and Δx = . Let [a, b] be divided into n subintervals, each of
n

length Δx , with endpoints at P = {x0 , x1 , x2 , … , xn }. Set


Δx
Sn = (f(x0 ) + 4 f(x1 ) + 2 f(x2 ) + 4 f(x3 ) + 2 f(x4 ) + ⋯ + 2 f(xn−2 ) + 4 f(xn−1 ) + f(xn )). (2.5.9)
3

Then,
b

lim Sn = ∫ f(x) dx.


n→+∞
a

Just as the trapezoidal rule is the average of the left-hand and right-hand rules for estimating definite integrals, Simpson’s rule may be obtained from the
2 1
midpoint and trapezoidal rules by using a weighted average. It can be shown that S 2n
= ( )Mn + ( )Tn .
3 3

12/21/2020 2.5.6 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269


It is also possible to put a bound on the error when using Simpson’s rule to approximate a definite integral. The bound in the error is given by the
following rule:

Rule: Error Bound for Simpson’s Rule


Let f(x) be a continuous function over [a, b] having a fourth derivative, f
(4)
(x) , over this interval. If M is the maximum value of ∣f (4) (x)∣
∣ ∣ over
b

[a, b] , then the upper bound for the error in using S to estimate ∫ n
f(x) dx is given by
a

5
M (b − a)
Error in Sn ≤ . (2.5.10)
4
180n

Example 2.5.7 : Applying Simpson’s Rule 1


1

Use S to approximate ∫
2
3
x dx . Estimate a bound for the error in § . 2

Solution
Since [0, 1] is divided into two intervals, each subinterval has length . The endpoints of these subintervals are . If we set
1−0 1 1
Δx = = {0, , 1}
2 2 2

f(x) = x , then
3

1 1 1 1 1 1
S4 = ⋅ (f(0) + 4 f( ) + f(1)) = (0 + 4 ⋅ + 1) = .
3 2 2 6 8 4

Since f (4)
(x) = 0 and consequently M = 0, we see that
5
0(1)
Error in S 2

4
= 0.
180⋅2

1
1
This bound indicates that the value obtained through Simpson’s rule is exact. A quick check will verify that, in fact, ∫ 3
x dx = .
0
4

Example 2.5.8 : Applying Simpson’s Rule 2


Use S to estimate the length of the curve y =
6
1

2
x
2
over [1, 4].
Solution
4
−−−− −
The length of y =
1

2
x
2
over [1, 4] is ∫
2
√1 + x dx . If we divide [1, 4] into six subintervals, then each subinterval has length Δx =
4−1

6
=
1

2
,
1
−−−− −
and the endpoints of the subintervals are {1, 3

2
, 2,
5

2
, 3,
7

2
, 4} . Setting f(x) = √1 + x
2
,
1 1 3 5 7
S6 = ⋅ (f(1) + 4f( ) + 2f(2) + 4f( ) + 2f(3) + 4f( ) + f(4)).
3 2 2 2 2

After substituting, we have


1
S6 = (1.4142 + 4 ⋅ 1.80278 + 2 ⋅ 2.23607 + 4 ⋅ 2.69258 + 2 ⋅ 3.16228 + 4 ⋅ 3.64005 + 4.12311) ≈ 8.14594 units. (2.5.11)
6

Exercise 2.5.5
2
1
Use S to estimate ∫
2 dx.
1
x

Hint
1
S2 = ( Δx(f(x0 ) + 4f(x1 ) + f(x2 )) (2.5.12)
3

Answer
25

36

Key Concepts
We can use numerical integration to estimate the values of definite integrals when a closed form of the integral is difficult to find or when an
approximate value only of the definite integral is needed.
The most commonly used techniques for numerical integration are the midpoint rule, trapezoidal rule, and Simpson’s rule.
The midpoint rule approximates the definite integral using rectangular regions whereas the trapezoidal rule approximates the definite integral
using trapezoidal approximations.
Simpson’s rule approximates the definite integral by first approximating the original function using piecewise quadratic functions.

Key Equations

12/21/2020 2.5.7 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269


Midpoint rule
n

Mn = ∑ f(mi )Δx

i=1

Trapezoidal rule
Δx
Tn = (f(x0 ) + 2 f(x1 ) + 2 f(x2 ) + ⋯ + 2 f(xn−1 ) + f(xn ))
2

Simpson’s rule
Δx
Sn = (f(x0 ) + 4 f(x1 ) + 2 f(x2 ) + 4 f(x3 ) + 2 f(x4 ) + 4 f(x5 ) + ⋯ + 2 f(xn−2 ) + 4 f(xn−1 ) + f(xn ))
3

Error bound for midpoint rule


3
M (b − a)
Error in M n

2
24n

Error bound for trapezoidal rule


3
M (b − a)
Error in T n ≤
12n2

Error bound for Simpson’s rule


5
M (b − a)
Error in S n ≤
4
180n

Glossary
absolute error
if B is an estimate of some quantity having an actual value of A, then the absolute error is given by |A − B|

midpoint rule
n b

a rule that uses a Riemann sum of the form M n


= ∑ f(mi )Δx , where m is the midpoint of the i
i
th
subinterval to approximate ∫ f(x) dx
a
i=1

numerical integration
the variety of numerical methods used to estimate the value of a definite integral, including the midpoint rule, trapezoidal rule, and Simpson’s rule

relative error
error as a percentage of the actual value, given by
A−B
relative error =∣ ∣ ⋅100%
A

Simpson’s rule
b

a rule that approximates ∫ f(x) dx using the area under a piecewise quadratic function.
a
b

The approximation S to ∫ n f(x) dx is given by


a

Δx
Sn = (f(x0 ) + 4 f(x1 ) + 2 f(x2 ) + 4 f(x3 ) + 2 f(x4 ) + ⋯ + 2 f(xn−2 ) + 4 f(xn−1 ) + f(xn )).
3

trapezoidal rule
b

a rule that approximates ∫ f(x) dx using the area of trapezoids.


a
b

The approximation T to ∫ n
f(x) dx is given by
a

Δx
Tn = (f(x0 ) + 2 f(x1 ) + 2 f(x2 ) + ⋯ + 2 f(xn−1 ) + f(xn )).
2

Contributors

Gilbert Strang (MIT) and Edwin “Jed” Herman (Harvey Mudd) with many contributing authors. This content by OpenStax is licensed with a CC-BY-
SA-NC 4.0 license. Download for free at http://cnx.org.
Edited by Paul Seeburger (Monroe Community College). Notes added to development of area under a parabola and typos fixed in original text.

12/21/2020 2.5.8 CC-BY-NC-SA https://math.libretexts.org/@go/page/10269

You might also like