Lecture 5: Numerical Integration: Discretizing The Integral
Lecture 5: Numerical Integration: Discretizing The Integral
_
y
0
e
x
2
dx . (1)
To leave the analysis in its most general form, we will consider an evaluation of the integral
_
b
a
f(x) dx . (2)
This integral is evaluated numerically by splitting up the domain [a, b] into N equally spaced
intervals as shown in Figure 1. Because we assume that the intervals are constant, then the
interval width is given by
Figure 1: Discretization of a function f(x) into N = 8 equally spaced subintervals over [a, b].
h = x = x
i+1
x
i
. (3)
The idea behind the numerical integration formulas is to approximate the integral in each
subinterval and add up the N approximate integrals to obtain the integral over [a, b].
Handout 6 06/08/02 2
Trapezoidal rule
The Trapezoidal rule approximates the function within each subinterval using the rst term
in the Taylor series expansion about x
i
, such that, in the range [x
i
, x
i+1
],
f(x) = f
i
+ (x x
i
)f
i
+
1
2
(x x
i
)
2
f
i
+O
_
(x x
i
)
3
_
. (4)
Using this approximation, we can evaluate the integral over [x
i
, x
i+1
] with
_
x
i+1
x
i
f(x) dx =
_
x
i+1
x
i
f
i
+ (x x
i
)f
i
+
1
2
(x x
i
)
2
f
i
dx , (5)
where we have omitted the truncation error term since the last term will end up being the
error term in the analysis. Making a change of variables such that
s =
x x
i
x
i+1
x
i
=
x x
i
h
, (6)
we have
_
x
i+1
x
i
f(x) dx = h
_
1
0
f
i
+ hsf
i
+
1
2
h
2
s
2
f
i
ds ,
= hsf
i
+
1
2
h
2
s
2
f
i
+
1
6
h
3
s
3
f
1
0
,
= hf
i
+
1
2
h
2
f
i
+
1
6
h
3
f
i
.
Substituting in an approximation for the rst derivative
f
i
=
f
i+1
f
i
h
h
2
f
i
, (7)
we have
_
x
i+1
x
i
f(x) dx = hf
i
+
1
2
h
2
_
f
i+1
f
i
h
h
2
f
i
_
+
1
6
h
3
f
i
,
=
1
2
h(f
i
+ f
i+1
)
1
12
h
3
f
i
. (8)
Which shows that the Trapezoidal rule approximates the integral of the function over the
subinterval [x
i
, x
i+1
] as the area of the trapezoid created by the function values at f
i
and
f
i+1
, as shown in Figure 2.
The integral over [a, b] is evaluated by taking the sum of the approximate integrals eval-
uated in each subinterval as
_
b
a
f(x) dx =
N1
i=0
_
x
i+1
x
i
f(x) dx ,
=
N1
i=0
_
1
2
h(f
i
+ f
i+1
)
1
12
h
3
f
i
_
,
=
1
2
h(f
0
+ 2f
1
+ 2f
2
+ . . . + 2f
N2
+ 2f
N1
+ f
N
)
h
3
12
N1
i=0
f
i
.
Handout 6 06/08/02 3
Figure 2: Depiction of how the trapezoidal rule approximates the integral on the subinterval
[x
i
, x
i+1
].
The error term is given by
Error =
h
3
12
_
f
0
+ f
1
+ f
2
+ . . . + f
N1
_
,
=
Nh
3
12
_
f
0
+ f
1
+ f
2
+ . . . + f
N1
N
_
.
If the mean value of f
i
is given by
_
f
0
+ f
1
+ f
2
+ . . . + f
N1
N
_
, (9)
then we know that it must lie within the bounds of f
() =
_
f
0
+ f
1
+ f
2
+ . . . + f
N1
N
_
. (10)
Therefore, since Nh = (b a), the error becomes
Error =
(b a)h
2
12
f
() = O
_
h
2
_
, (11)
which shows that the trapezoidal rule is second order accurate.
Simpsons rules
Simpsons 1/3 rule
Simpsons 1/3 rule approximates the function within the interval [x
i
, x
i+2
] as a quadratic,
as shown in Figure 3. This is done by writing the Taylor series expansion of f(x) about
Handout 6 06/08/02 4
Figure 3: Depiction of how the Simpsons 1/3 rule approximates the function f(x) with a
quadratic through x
i
, x
i+1
, and x
i+2
.
x = x
i+1
to obtain
f(x) = f
i+1
+ (x x
i+1
)f
i+1
+
1
2
(x x
i+1
)
2
f
i+1
+
1
6
(x x
i+1
)
3
f
i+1
+
1
24
(x x
i+1
)
4
f
(iv)
i+1
+O
_
(x x
i+1
)
5
_
.
The integral in the subinterval [x
i
, x
i+2
] is then given by
_
x
i+2
x
i
f(x) dx =
_
x
i+2
x
i
f
i+1
+ (x x
i+1
)f
i+1
+
1
2
(x x
i+1
)
2
f
i+1
+
1
6
(x x
i+1
)
3
f
i+1
+
1
24
(x x
i+1
)
4
f
(iv)
i+1
dx ,
where the truncation error has been left o since the last term will end up being the error.
Making a change of variables such that
s =
2(x x
i+1
)
x
i+2
x
i
=
x x
i+1
h
, (12)
we have
_
x
i+2
x
i
f(x) dx = h
_
+1
1
f
i+1
+ hsf
i+1
+
1
2
h
2
s
2
f
i+1
+
1
6
h
3
s
3
f
i+1
+
1
24
h
4
s
4
f
(iv)
i+1
ds , (13)
which becomes
_
x
i+2
x
i
f(x) dx = hsf
i+1
+
1
2
h
2
s
2
f
i+1
+
1
6
h
3
s
3
f
i+1
+
1
24
h
4
s
4
f
i+1
+
1
120
h
5
s
5
f
(iv)
i+1
+1
1
,
= 2hf
i+1
+
1
3
h
3
f
i+1
+
1
60
h
5
f
(iv)
i+1
.
Handout 6 06/08/02 5
Using the second order accurate approximation to the second derivative
f
i+1
=
f
i
2f
i+1
+ f
i+2
h
2
h
2
12
f
(iv)
i+1
, (14)
the integral becomes
_
x
i+2
x
i
f(x) dx = 2hf
i+1
+
1
3
h
3
_
f
i
2f
i+1
+ f
i+2
h
2
h
2
12
f
(iv)
i+1
_
+
1
60
h
5
f
(iv)
i+1
.
=
1
3
h(f
i
+ 4f
i+1
+ f
i+2
)
1
90
h
5
f
(iv)
i+1
. (15)
The integral over [a, b] is taken by taking the sum of the approximate integrals, as in
_
b
a
f(x) dx =
N/2
i=0
_
x
i+2
x
i
f(x) dx ,
=
N/2
i=0
_
1
3
h(f
i
+ 4f
i+1
+ f
i+2
)
1
90
h
5
f
(iv)
i+1
_
.
The sum is given by
1
3
h( f
0
+ 4f
1
+ f
2
+
f
2
+ 4f
3
+ f
4
+
f
4
+ 4f
5
+ f
6
+ . . . +
f
N6
+ 4f
N5
+ f
N4
+
f
N4
+ 4f
N3
+ f
N2
+
f
N2
+ 4f
N1
+ f
N
) ,
which becomes
_
b
a
f(x) dx =
1
3
(f
0
+ 4f
1
+ 2f
2
+ 4f
3
+ . . . + 4f
N3
+ 2f
N2
+ 4f
N1
+ f
N
)
1
90
h
5
N/2
i=0
f
(iv)
i+1
.
The error term is given by
Error =
1
90
h
5
N/2
i=0
f
(iv)
i+1
, (16)
which, using the same arguments as those for the Trapezoidal rule, becomes
Error =
1
180
(b a)h
4
f
(iv)
() = O
_
h
4
_
, (17)
which shows that Simpsons 1/3 rule is fourth order accurate.
Handout 6 06/08/02 6
Simpsons 3/8 rule
Simpsons 3/8 rule approximates the function within the subinterval [x
i
, x
i+3
] using a quartic.
The Taylor series expansion is performed about x
i+3/2
to obtain
f(x) = f
i+3/2
+ (x x
i+3/2
)f
i+3/2
+
1
2
(x x
i+3/2
)
2
f
i+3/2
+
1
6
(x x
i+3/2
)
3
f
i+3/2
+
1
24
(x x
i+3/2
)
4
f
(
iv)
i+3/2
+O
_
(x x
i+3/2
)
5
_
. (18)
Integrating this function in a similar manner to that used for the 1/3 rule yields
_
b
a
f(x) dx =
3
8
h(f
0
+ 3f
1
+ 3f
2
+ 2f
3
+ 3f
4
+ 3f
5
+ . . .
+ 2f
N3
+ 3f
N2
+ 3f
N1
+ f
N
) (19)
1
80
(b a)h
4
f
(iv)
() .
Summary of integration formulas and pseudocodes
Trapezoidal rule
_
b
a
f(x) dx =
1
2
h(f
0
+ 2f
1
+ 2f
2
+ . . . + 2f
N2
+ 2f
N1
+ f
N
) + Error
Error =
1
12
(b a)h
2
f
() = O
_
h
2
_
1. If f
i
and h are already known discretely on an equispaced grid with N + 1 points,
then proceed to step 2.
Otherwise, choose interval [a, b] and set h = (b a)/N.
for i = 1 to N + 1
Set x
i
= a + h(i 1)
Set f
i
= f(x
i
)
end
2. Set I = 0
for i = 2 to N
Set I = I + hf
i
end
Set I = I +
1
2
h(f
1
+ f
N+1
)
3. The integral is given by I.
Handout 6 06/08/02 7
Simpsons 1/3 rule (N divisible by 2)
_
b
a
f(x) dx =
1
3
(f
0
+ 4f
1
+ 2f
2
+ 4f
3
+ . . . + 4f
N3
+ 2f
N2
+ 4f
N1
+ f
N
)
+ Error
Error =
1
180
(b a)h
4
f
(iv)
() = O
_
h
4
_
1. If f
i
and h are already known discretely on an equispaced grid with N + 1 points,
where N is even, then proceed to step 2.
Otherwise, choose interval [a, b] and set h = (b a)/N, with N even.
for i = 1 to N + 1
Set x
i
= a + h(i 1)
Set f
i
= f(x
i
)
end
2. Set I = 0
for i = 1 to
N
2
Set I = I +
4
3
hf
2i
end
for i = 1 to
N
2
1
Set I = I +
2
3
hf
2i+1
end
Set I = I +
1
3
h(f
1
+ f
N+1
)
3. The integral is given by I.
Handout 6 06/08/02 8
Simpsons 3/8 rule (N divisible by 3)
_
b
a
f(x) dx =
3
8
h(f
0
+ 3f
1
+ 3f
2
+ 2f
3
+ 3f
4
+ 3f
5
+ . . .
+ 2f
N3
+ 3f
N2
+ 3f
N1
+ f
N
) (20)
+ Error
Error =
1
80
(b a)h
4
f
(iv)
() .
1. If f
i
and h are already known discretely on an equispaced grid with N + 1 points,
where N is divisible by 3, then proceed to step 2.
Otherwise, choose interval [a, b] and set h = (b a)/N, with N divisible by 3.
for i = 1 to N + 1
Set x
i
= a + h(i 1)
Set f
i
= f(x
i
)
end
2. Set I = 0
for i = 2 to N
Set I = I +
9
8
hf
i
end
for i = 1 to
N
3
1
Set I = I
3
8
hf
3i+1
end
Set I = I +
3
8
h(f
1
+ f
N+1
)
3. The integral is given by I.