Professional Documents
Culture Documents
It is very common to encounter integrals that are too complicated to evaluate explicitly.
We now study how to find (approximate) numerical values for integrals, without having to
evaluate them algebraically.
y = f (x)
x
a = x0 x1 x2 x3 · · · xn−1 xn = b
x1 = a + ∆x. The second runs from x1 to x2 = a + 2∆x, and so on. The last runs from
xn−1 = b − ∆x to xn = b. The corresponding decomposition of the integral is
Z b Z x1 Z x2 Z xn
f (x) dx = f (x) dx + f (x) dx + · · · + f (x) dx
a x0 x1 xn−1
R xj
Each subintegral xj−1 f (x) dx is approximated by the area of a simple geometric figure. The
three different algorithms use three different figures.
f (xj ) f
xj−1 +xj
2
f (xj−1 )
Example 1
Rπ
Let’s apply the midpoint rule with n = 8 steps to the integral 0
sin x dx. First note that
a = 0, b = π, ∆x = π8 and
π 2π 7π 8π
x0 = 0 x1 = 8
x2 = 8
··· x7 = 8
x8 = 8
=π
Consequently,
π 3π 13π 15π
x̄1 = 16
x̄2 = 16
··· x̄7 = 16
x̄8 = 16
and
Z π h i
sin x dx ≈ sin(x̄1 ) + sin(x̄2 ) + · · · + sin(x̄8 ) ∆x
0
h i
π
= sin( 16 ) + sin( 3π
16
) + sin( 5π
16
) + sin( 7π
16
) + sin( 9π
16
) + sin( 11π
16
) + sin( 13π
16
) + sin( 15π
16
) π
8
h i
= 0.1951 + 0.5556 + 0.8315 + 0.9808 + 0.9808 + 0.8315 + 0.5556 + 0.1951 × 0.3927
= 5.1260 × 0.3927
= 2.013
Rπ π
The exact answer is 0
sin x dx = − cos x = 2. So with eight steps of the midpoint rule we
0
achieved an absolute error of |2.013 − 2| = 0.013, a relative error of |2.013−2|
2
= 0.0065 and a
2.013−2
percentage error of 100 2 = 0.65%. The definitions of these various types of error are
given in Definition 2, below.
Example 1
f (xj ) f (xj )
f (xj−1 ) f (xj−1 )
xj−1 xj xj−1 xj
approximates Z xj
f (xj−1 )+f (xj )
f (x) dx ≈ 2
∆x
xj−1
where
b−a
∆x = n
, x0 = a, x1 = a + ∆x, x2 = a + 2∆x, ··· , xn−1 = b − ∆x, xn = b
Consequently,
Z π h i
sin x dx ≈ 21 sin(x0 ) + sin(x1 ) + · · · + sin(x7 ) + 21 sin(x8 ) ∆x
0
h i
1 π 2π 3π 4π 5π 6π 7π 1 8π π
= 2 sin 0 + sin 8 + sin 8 + sin 8 + sin 8 + sin 8 + sin 8 + sin 8 + 2 sin 8 8
h i
= 21 ×0 + 0.3827 + 0.7071 + 0.9239 + 1.0000 + 0.9239 + 0.7071 + 0.3827 + 21 ×0 × 0.3927
= 5.0274 × 0.3927
= 1.974
Rπ π
The exact answer is 0
sin x dx = − cos x = 2. So with eight steps of the trapezoidal rule
0
|1.974−2|
we achieved 100 2 = 1.3% accuracy.
Example 3
Simpson’s Rule
Rx
Simpson’s rule approximates x02 f (x) dx by the area bounded by the x–axis, the parabola
that passes through the three points x0 , f (x0 ) , x1 , f (x1 ) and x2 , f (x2 ) , the vertical line
Rx
x = x0 and the vertical line x = x2 . It then approximates x24 f (x) dx by the area between
x1 , f (x1 )
x2 , f (x2 )
x0 , f (x0 )
x0 x1 x2
the x–axis and the part of a parabola with x2 ≤ x ≤ x4 . This parabola passes through the
three points x2 , f (x2 ) , x3 , f (x3 ) and x4 , f (x4 ) . And so on. Because Simspon’s rule
does the approximation two slices at a time, n must be even.
To derive Simpson’s rule formula, we first find the equation of the parabola that passes
through the three points x0 , f (x0 ) , x1 , f (x1 ) and x2 , f (x2 ) . Then we find the area
and Z x4
f (x) dx ≈ 13 ∆x f (x2 ) + 4f (x3 ) + f (x4 )
x2
and so on. All together
Z b Z x2 Z x4 Z x6 Z xn
f (x) dx = f (x) dx + f (x) dx + f (x) dx + · · · + f (x) dx
a x0 x2 x4 xn−2
∆x
f (x0 ) + 4f (x1 ) + f (x2 ) + ∆x
≈ 3 3
f (x2 ) + 4f (x3 ) + f (x4 )
+ ∆x ∆x
3
f (x4 ) + 4f (x5 ) + f (x6 ) + · · · + 3
f (xn−2 ) + 4f (xn−1 ) + f (xn )
h i
= f (x0 )+ 4f (x1 )+ 2f (x2 )+ 4f (x3 )+ 2f (x4 )+ · · · + 2f (xn−2 )+ 4f (xn−1 )+ f (xn ) ∆x
3
Example 4
Rπ
As an example we approximate 0 sin x dx with n = 8, yet again. Under Simpson’s rule
Z π h i
sin x dx ≈ sin(x0 ) + 4 sin(x1 ) + 2 sin(x2 ) + · · · + 4 sin(x7 ) + sin(x8 ) ∆x 3
0
h
= sin(0) + 4 sin( π8 ) + 2 sin( 2π
8
) + 4 sin( 3π
8
) + 2 sin( 4π8
)
i
+ 4 sin( 5π
8
) + 2 sin( 6π
8
) + 4 sin( 7π
8
) + sin( 8π
8
π
) 8×3
h
= 0 + 4 × 0.382683 + 2 × 0.707107 + 4 × 0.923880 + 2 × 1.0
i
π
+ 4 × 0.923880 + 2 × 0.707107 + 4 × 0.382683 + 0 8×3
= 15.280932 × 0.130900
= 2.00027
This completes our derivation of the midpoint, trapezoidal and Simpson’s rules for ap-
proximating the values of definite integrals. So far we have not attempted to see how efficient
and how accurate the algorithms are. That’s our next task.
Observe that
• Using 101 evaluations of f worth of Simpson’s rule gives an error 80 times smaller than
1000 evaluations of f worth of the midpoint rule.
• The trapezoidal rule error with n steps is about twice the midpoint rule error with n
steps.
• With the midpoint rule, increasing the number of steps by a factor of 10 appears to
reduce the error by about a factor of 100 = 102 = n2 .
• With the trapezoidal rule, increasing the number of steps by a factor of 10 appears to
reduce the error by about a factor of 102 = n2 .
• With Simpson’s rule, increasing the number of steps by a factor of 10 appears to reduce
the error by about a factor of 104 = n4 .
So it looks like
Rb Rb
approx value of a f (x) dx given by n midpoint steps ≈ a f (x) dx + KM n12
Rb Rb
approx value of a f (x) dx given by n trapezoidal steps ≈ a f (x) dx + KT n12
Rb Rb
approx value of a f (x) dx given by n Simpson’s steps ≈ a f (x) dx + KM n14
en = K n1k
for some constants K and k. We would like to test if this is really the case. It is not easy to
tell whether or not a given curve really is a parabola y = x2 or a quartic y = x4 . But the eye
is pretty good at determining whether or not a graph is a straight line. Fortunately, there is
a little trick that turns the curve en = K n1k into a straight line – no matter what k is. Instead
1 2 3 4 5 6 7 8 9 10 12 −4
x = log2 n −6
−2 −8 ■
−4 ■
−10
■ trapezoidal rule
−6 y = 0.7253 − 2.0008 x
■
−12 ■
−8 ■
−14
■
−10 ■
−16 ■
−12 ■
−18
■
−14 ■
−20 ■
−16 ■
−22
■
−18 ■
−24 ■
−20
■
Simpson’s rule
■
−26 y = 0.35 − 4.03 x
■
−22 ■
−28 ■
−24 ■
−30
midpoint rule ■
−28 −34
y = log2 en
−36 ■
−38
−40 ■
y = log2 en
Rπ
Figure 1: Error in the approximation, with n steps, to 0
sin x dx
of plotting en against n, plot log en against log n. If en = K n1k , then log en = log K − k log n.
So plotting y = log en against x = log n gives the straight line y = log K − kx, which has
slope −k and y–intercept log K.1
The three graphs in Figure 1 plot y = log2 en against x = log2 n for our three rules. By
definition, the base 2 logarithm, log2 n, is the power to which 2 must be raised to give n.
1
There is a variant of this trick that works even when you don’t know the answer to the integral ahead of
time. Suppose that you suspect that the approximation Mn = A + K n1k , where A is the exact value of the
integral and suppose that you don’t know the values of A, K and k. Then Mn − M2n = K n1k − K (2n) 1
k =
1 1 1
K 1− 2k nk , so plotting y = log(Mn −M2n ) against x = log n gives the straight line y = log K 1− 2k −kx.
M (b−a)5
the total error introduced by Simpson’s rule is bounded by 180 n4
The first of these error bounds in proven in the following (optional) section. Here are some
examples which illustrate how they are used.
Example 5
Rπ 2
The integral 0 sin x dx has b − a = π and M, the largest possible value of ddx2 sin x (for the
4
midpoint and trapezoidal rules) or ddx4 sin x (for Simpson’s rule) is 1. So, for the midpoint
rule, the error, en , introduced when n steps are used is bounded by
M (b−a)3 π3 1
|en | ≤ 24 n2
= 24 n2
≈ 1.29 n12
The data in the graph in Figure 1 gives |en | ≈ 2−.2706 n12 = 0.83 n12 which is consistent with
3
the bound |en | ≤ π24 n12 .
Example 5
Example 6
In a typical application, one is required to evaluate a given integral to some specified accu-
racy. For example, if you are manufacturer and your machinery can only cut materials to
1 th
an accuracy of 10 of a millimeter, there is no point in making design specifications more
1 th
accurate than 10 of a millimeter. Suppose, for example, that we wish to use the midpoint
R1 2
rule to evaluate 0 e−x dx to within an accuracy of 10−6 . (In fact this integral cannot be
Example 7
R1 2
Suppose now that we wish to use Simpson’s rule to evaluate 0 e−x dx to within an accuracy
d4 −x2
of 10−6 . To determine the number of steps required, we first determine how big dx 4e can
get when 0 ≤ x ≤ 1.
d3 −x2 2 2 2
d
2(2x2 − 1)e−x = 8xe−x − 4x(2x2 − 1)e−x
dx3
e = dx
2
= 4(−2x3 + 3x)e−x
d4 −x2 2 2 2
d
4(−2x3 + 3x)e−x = 4(−6x2 + 3)e−x − 8x(−2x3 + 3x)e−x
dx4
e = dx
2
= 4(4x4 − 12x2 + 3)e−x
2
On the domain of integration 0 ≤ x ≤ 1 so that e−x ≤ 1. Also, for 0 ≤ x ≤ 1,
Consequently, the maximum value of |4x4 − 12x2 + 3| for 0 ≤ x ≤ 1 is no more than 9 and
d4 −x2
4x4 − 12x2 + 3 ≤ 9 =⇒ dx4
e ≤ 4 × 9 × 1 = 36
5
M (b−a) 36 (1−0)5 1
The error introduced by the n step Simpson’s rule is at most 180 n4
≤ 180 n4
= 5n4
.
−6
This error is at most 10 if
q
1 −6 4 1 6 4 1
5n4
≤ 10 ⇐⇒ n ≥ 5
× 10 ⇐⇒ n ≥ 5
× 106 = 21.1
x0
We can get a pretty good idea as to how big this error is by using the quadratic approximation
to f (x) about x̄1 :
In fact we can do even better than this. There is an exact formula for f (x) that looks a lot
like the quadratic approximation.
Here c(x) is some (unknown) number that depends on x and is somewhere between x̄1 and
x. Subbing this exact formula into the integral gives
Z x1 Z x1 Z x1 Z x1
′ 1
f ′′ c(x) (x − x̄1 )2 dx
f (x) dx = f (x̄1 ) 1 dx + f (x̄1 ) (x − x̄1 ) dx + 2
x0 x0 x0
x1 x1
Z x1 x0
= f (x̄1 ) x + f ′ (x̄1 ) 12 (x − x̄1 )2 + 21 f ′′ c(x) (x − x̄1 )2 dx
x0 x0 x0
Z x1
= f (x̄1 )∆x + 21 f ′′ c(x) (x − x̄1 )2 dx
x0
2
since x1 − x0 = ∆x and (x1 − x̄1 )2 = (x0 − x̄1 )2 = ∆x 2
. Note that the first term on the
right hand side is exactly the midpoint rule approximation. So the error that the midpoint
rule introduces in the first slice is exactly
Z x1 Z x1
1
f ′′ c(x) (x − x̄1 )2 dx
f (x) dx − f (x̄1 )∆x = 2
x0 x0
Even though we don’t know exactly what c(x) is, this is a still very useful formula if we
know that |f ′′ (x)| is smaller than some number M for all x in the domain of integration.
For example, when f (x) = sin x, as in the example that we have been using, we know that
|f ′′(x)| = | − sin x| ≤ M = 1 for all x. Then, |f ′′ c(x) | ≤ M, no matter what c(x) is, and
Z x1 Z x1 x1
f (x) dx − f (x̄1 )∆x ≤ 1
2
M(x − x̄1 )2 dx ≤ 12 M 31 (x − x̄1 )3
x0 x0
hx0 i h i
3 3 3 ∆x 3
= M6 x1 − x̄1 − x0 − x̄1 = M6 ∆x
2
− − 2
= M
24
(∆x)3
We have just shown that, if |f ′′ (x)| ≤ M for all x in the domain of integration, then
M (b−a)3
the total error introduced by the midpoint rule is bounded by 24 n2
h
1
k A(h/2) − A(h)
(6a) − (6b) : 0 = A(h) − A 2
+ 1− 2k
Kh =⇒ K= (7a)
[1 − 2−k ]hk
2k A(h/2) − A(h)
2k (6b) − (6a) : [2k − 1]A = 2k A h
2
− A(h) =⇒ A= (7b)
2k − 1
The generation of a “new improved” approximation for A from two A(h)’s with different
values of h is called Richardson Extrapolation.
Example 8
Rπ π π
Applying the trapezoid rule to 0
sin x dx with step sizes 8
and 16
gives, with h = π8 ,
h
A(h) = 1.9742316 A 2
= 1.9935703
22 × 1.99357034 − 1.97423160
= 2.0000166
22 − 1
and (7a) tells us that
1.99357034 − 1.97423160 0.258
K= −2 2
= π 2
[1 − 2 ]( /8)
π ( /8)
so that, approximately, the error in A(h) is K(π/8)2 = 0.2578 and the error in A h/2 is
k
K(π/16)2 = 0.2578/4 = 0.0064. Beware that (6b) is saying that K h/2 is (approximately) the
error in A h/2 , not the error in A. You cannot get an “even more improved” approximation
k
by using (7a) to compute K and then adding K h/2 to the “new improved” approximation
(7b).
Example 8
Example 9
R1 2
(Example 7 revisited) Suppose again that we wish to use Simpson’s rule to evaluate 0 e−x dx
to within an accuracy of 10−6 , but that we do not need the degree of certainty provided by
Example 7. A commonly used strategy is to
• first apply Simpson’s rule twice with some relatively small number of steps and
• then use the condition |K|hk ≤ 10−6 to determine, approximately, the number of steps
required
• and finally apply Simpson’s rule with the number of steps just determined.
Once again, suppose that we have chosen some h and that we have evaluated A(h) and
A(h/2). They obey
Note that, in equations (8a) and (8b), the symbol “O(hk+2)” is used to stand for two different
functions. If we denote by g(h) the function that is represented by the “O(hk+2)” in (8a),
then the function that is represented by the “O(hk+2)” in (8b), is g h2 . As |g(h)| ≤ C|h|k+2
k+2
we have g h2 ≤ C h2 = C ′ |h|k+2 where C ′ = C/2k+2 , Since g h2 is bounded by some
(This O(hk+2) is yet a third different function, namely 2k g h2 − g(h), that is bounded by
I = T (h) + K1 h2 + K2 h4 + K3 h6 + · · ·
We know another method which produces an error of order 4 — Simpson’s rule. In fact,
T1 (h) is exactly Simpson’s rule (for step size h2 ).
Example 10
The following table illustrates Romberg integration by applying it to the integral A =
Rπ
0
sin x dx = 2.
The next table gives the percentage error for each of the entries in the above table.
h 100 2−T2 (h) 100 2−T21 (h) 100 2−T22 (h) 100 2−T23 (h)
π/4 5.2 −1.3 × 10−2 1.2 × 10−5 −2.9 × 10−9
π/8 1.3 −8.3 × 10−4 1.9 × 10−7
π/16 0.32 −5.2 × 10−5
π/32 0.08
Example 10
Example 11
with
S a, a+b h h h
+ f a+b
2
; 2
= 6
f (a) + 4f a + 2 2
a+b h h
f a+b a + 3h
S 2
,b; 2
= 6 2
+ 4f 2
+ f (b)
Using the Richardson extrapolation formula (7a) with k = 4 gives that the error in
S a, b ; h2 is (approximately)
4
K h2 1
S a, b ; h2 − S a, b ; h
= 15 (9)
If this is smaller than ε, we have (approximately) the desired accuracy and stop. (It is
4
very common to build in a bit of a safety margin and require that, for example, K h2
be smaller than 2ε rather than ε.)
Example 12
Let’s apply adaptive quadrature using Simpson’s rule as above with the goal of computing
R1√
0
x dx with an error of at most ε = 0.0005 = 5 × 10−4 .
• Step 1 – the interval [0, 1]. (The notation [0, 1] stands for the interval 0 ≤ x ≤ 1.)
S(0, 1 ; 21 ) = 0.63807119
S(0, 21 ; 41 ) = 0.22559223
S( 21 , 1 ; 14 ) = 0.43093403
1
error = 15
S(0, 12 ; 14 ) + S( 21 , 1 ; 14 ) − S(0, 1 ; 21 ) = 0.0012 > ε = 0.0005
This is unacceptably large, so we subdivide the interval [0, 1] into the two halves 0, 21
S(0, 12 ; 14 ) = 0.22559223
S(0, 41 ; 18 ) = 0.07975890
S( 14 , 21 ; 18 ) = 0.15235819
1
error = 15
S(0, 14 ; 18 ) + S( 14 , 21 ; 18 ) − S(0, 12 ; 41 ) = 0.00043 > ε
2
= 0.00025
S( 21 , 1 ; 14 ) = 0.43093403
S( 12 , 43 ; 18 ) = 0.19730874
S( 43 , 1 ; 18 ) = 0.23365345
1
error = 15
S( 21 , 34 ; 18 ) + S( 43 , 1 ; 18 ) − S( 21 , 1 ; 41 ) = 0.0000019 < ε
2
= 0.00025
• Step 2 resumé. The error for the interval [0, 21 ] is unacceptably large, so we subdivide
the interval [0, 12 ] into the two halves [0, 41 ] and [ 14 , 21 ] and apply the procedure separately
to each half. The error for the interval [ 21 , 1] is small enough, so we accept
S( 21 , 1 ; 81 ) = S( 12 , 43 ; 18 ) + S( 43 , 1 ; 18 ) = 0.43096219
R1 √
as the approximate value of 1/2 x dx.
• Step 3 resumé. The error for the interval [0, 14 ] is unacceptably large, so we subdivide
the interval [0, 14 ] into the two halves [0, 81 ] and [ 18 , 41 ] and apply the procedure separately
to each half. The error for the interval [ 41 , 21 ] is small enough, so we accept
S( 14 , 21 ; 16
1
) = S( 41 , 83 ; 16
1
) + S( 38 , 21 ; 16
1
) = 0.15236814
R 1/2 √
as the approximate value of 1/4 x dx.
• Step 4 resumé. The error for the interval [0, 18 ] is small enough, so we accept
S(0, 18 ; 32
1
) = S(0, 161 1
; 32 1 1 1
) + S( 16 , 8 ; 32 ) = 0.02901464
R 1/8 √
as the approximate value of 0 x dx. The error for the interval [ 18 , 41 ] is small enough,
so we accept
S( 18 , 41 ; 32
1
) = S( 81 , 16
3 1
; 32 3 1 1
) + S( 16 , 4 ; 32 ) = 0.05387027
R 1/4 √
as the approximate value of 1/8 x dx.
Example 12