2.5 Numerical Integration - Midpoint, Trapezoid, Simpson's Rule
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.
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] is obtained by selecting a partition
a
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, m , of each subinterval in place of x . Formally, we state a i
∗
i
n
subinterval, set
n
i=1
As we can see in Figure, if f (x) ≥ 0 over [a, b], then ∑ f (m i )Δx corresponds to the sum of the areas of rectangles approximating the area between the graph of f (x) and the x-axis
i=1
over [a, b]. The graph shows the rectangles corresponding to M for a nonnegative function over a closed interval [a, b].
4
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).
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
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
and we see that the midpoint rule produces an estimate that is somewhat close to the actual value of the definite integral.
2.5.1 https://math.libretexts.org/@go/page/10269
Example 2.5.2: Using the Midpoint Rule with M 6
2
x
2
on [1, 4].
Solution: The length of y = 1
2
2
x 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 {
5
4
,
7
4
,
9
4
,
11
4
,
13
4
,
15
4
} . If we set
6 2
−−−− −
f (x) = √1 + x
2
,
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
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 and parallel bases of length f (x ) and f (x ). Thus, the area of the first trapezoid in
1 2
1
2
1 2 0 1
Figure 2.5.2 is
1
Δx(f (x0 ) + f (x1 )).
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
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
Set
Δx
Tn = (f (x0 ) + 2 f (x1 ) + 2 f (x2 ) + ⋯ + 2 f (xn−1 ) + f (xn )). (2.5.2)
2
2.5.2 https://math.libretexts.org/@go/page/10269
b
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 examination of Figure 2.5.3 leads us
n n
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.
Solution
The endpoints of the subintervals consist of elements of the set P = {0,
1
4
,
1
2
,
3
4
, 1} and Δx = 1−0
4
=
1
4
. Thus,
1
2
1 1 1 1 3
∫ x dx ≈ ⋅ (f (0) + 2 f ( )+2 f ( )+2 f ( ) + 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
1
1
Solution: The calculated value is ∫ x
2
dx = and our estimate from the example is M 4 =
21
64
. Thus, the absolute error is given by ∣∣ 1
3
−
21
64
∣ =
∣
1
192
≈ 0.0052.
0
3
2.5.3 https://math.libretexts.org/@go/page/10269
Example 2.5.5: Calculating Error in the Trapezoidal Rule
1
1
1
Solution: The calculated value is ∫ 2
x dx = and our estimate from the example is T 4 =
11
32
. Thus, the absolute error is given by ∣∣ 1
3
−
11
32
∣ =
∣
1
96
≈ 0.0104.
0
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
2 ln 2 . Using 24
35
≈ 0.6857 and ln 2 ≈ 0.6931, calculate the absolute error
1
x
Hint
Use the previous examples as a guide.
Answer
absolute error ≈ 0.0074, and relative error ≈ 1.1%
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.
Solution
We begin by determining the value of M , the maximum value of |f over [0, 1] for f (x) = e . Since f '(x) = 2x e we have
2 2
′′ x x
(x)| ,
2 2
′′ x 2 x
f (x) = 2 e + 4x e .
Thus,
2
′′ x 2
|f (x)| = 2 e (1 + 2 x ) ≤ 2 ⋅ e ⋅ 3 = 6e.
−
−−−
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.
2.5.4 https://math.libretexts.org/@go/page/10269
Exercise 2.5.4
1
Use Equation to find an upper bound for the error in using M to estimate ∫ 4 x
2
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
x2 x2
do just this. We partition the interval into an even number of subintervals, each of equal width. Over the first pair of subintervals we approximate ∫ f (x) dx with ∫ ,
p(x) dx
x0 x0
where p(x) = Ax 2
+ Bx + C is the quadratic function passing through (x 0, f (x0 )), (x1 , f (x1 )), and (x 2, f (x2 )) (Figure 2.5.4). Over the next pair of subintervals we approximate
x4
∫ f (x) dx with the integral of another quadratic function passing through (x2 , f (x2 )), (x3 , f (x3 )), and (x4 , f (x4 )). This process is continued with each successive pair of
x2
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:
2
f (x0 ) = p(x0 ) = Ax + Bx0 + C (2.5.6)
0
2
f (x1 ) = p(x1 ) = Ax + Bx1 + C (2.5.7)
1
2
f (x2 ) = p(x2 ) = Ax + Bx2 + C (2.5.8)
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 x2 − x0
2 2 2 2
= ((Ax + Bx2 + C ) + (Ax + Bx0 + C ) + A(x + 2 x2 x0 + x ) + 2B(x2 + x0 ) + 4C ) Rearrange the terms. Note: Δx =
2 0 2 0
3 2
Δx 2
= (f (x2 ) + f (x0 ) + A(x2 + x0 ) + 2B(x2 + x0 ) + 4C ) Factor and substitute:
3
2 2
f (x2 ) = Ax + Bx2 + C and f (x0 ) = Ax + Bx0 + C .
2 0
Δx
2
= (f (x2 ) + f (x0 ) + A(2 x1 ) + 2B(2 x1 ) + 4C ) Substitute x2 + x0 = 2 x1 .
3
x2 + x0
Note: x1 = , the midpoint.
2
Δx
2
= (f (x2 ) + 4f (x1 ) + f (x0 )). Expand and substitute f (x1 ) = Ax + Bx1 + C .
1
3
2.5.5 https://math.libretexts.org/@go/page/10269
x4
4
x
Δx
∫ f (x) dx ≈ (f (x4 ) + 4 f (x3 ) + f (x2 )).
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 length Δx , with endpoints at
n
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
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 midpoint and trapezoidal rules
2 1
by using a weighted average. It can be shown that S 2n =( )Mn + ( )Tn .
3 3
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:
5
M (b − a)
Error in Sn ≤ . (2.5.10)
4
180n
Use S to approximate ∫
2 x
3
dx . Estimate a bound for the error in § . 2
Solution
Since [0, 1] is divided into two intervals, each subinterval has length Δx = 1−0
2
=
1
2
. The endpoints of these subintervals are {0, 1
2
, 1} . If we set f (x) = x 3
, then
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, ∫ x
3
dx = .
0
4
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
, and the endpoints of the
1
−−−− −
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
Exercise 2.5.5
2
1
Use S to estimate ∫
2 dx.
1
x
Hint
2.5.6 https://math.libretexts.org/@go/page/10269
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
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
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
Δ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
Δx
Tn = (f (x0 ) + 2 f (x1 ) + 2 f (x2 ) + ⋯ + 2 f (xn−1 ) + f (xn )).
2
2.5.7 https://math.libretexts.org/@go/page/10269
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.
2.5: Numerical Integration - Midpoint, Trapezoid, Simpson's rule is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.
2.5.8 https://math.libretexts.org/@go/page/10269