Lecture 5: Numerical integration

advertisement
Handout 6 06/08/02
1
Lecture 5: Numerical integration
Discretizing the integral
In this lecture we will derive methods of computing integrals of functions that cannot be
solved analytically. A typical function that cannot be solved analytically is the error function,
2 Z y −x2
Erf(y) = √
e
dx .
π 0
(1)
To leave the analysis in its most general form, we will consider an evaluation of the integral
Z
b
f (x) dx .
(2)
a
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 = xi+1 − xi .
(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 first term
in the Taylor series expansion about xi , such that, in the range [xi , xi+1 ],
1
f (x) = fi + (x − xi )fi0 + (x − xi )2 fi00 + O (x − xi )3 .
2
(4)
Using this approximation, we can evaluate the integral over [xi , xi+1 ] with
Z
xi+1
f (x) dx =
xi
Z
xi+1
xi
1
fi + (x − xi )fi0 + (x − xi )2 fi00 dx ,
2
(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 − xi
x − xi
=
,
xi+1 − xi
h
(6)
we have
xi+1
1
1
fi + hsfi0 + h2 s2 fi00 ds ,
2
xi
0
1 2 2 0 1 3 3 00 1
= hsfi + h s fi + h s fi ,
2
6
0
1 2 0 1 3 00
= hfi + h fi + h fi .
2
6
Substituting in an approximation for the first derivative
Z
f (x) dx = h
fi0 =
Z
fi+1 − fi h 00
− fi ,
h
2
(7)
we have
!
fi+1 − fi h 00
1
1
− fi + h3 fi00 ,
f (x) dx = hfi + h2
2
h
2
6
xi
1
1
h (fi + fi+1 ) − h3 fi00 .
(8)
=
2
12
Which shows that the Trapezoidal rule approximates the integral of the function over the
subinterval [xi , xi+1 ] as the area of the trapezoid created by the function values at fi and
fi+1 , as shown in Figure 2.
The integral over [a, b] is evaluated by taking the sum of the approximate integrals evaluated in each subinterval as
Z
Z
xi+1
b
f (x) dx =
a
=
N
−1 Z xi+1
X
i=0 xi
N
−1 X
i=0
=
f (x) dx ,
1
1
h (fi + fi+1 ) − h3 fi00 ,
2
12
−1
1
h3 NX
h (f0 + 2f1 + 2f2 + . . . + 2fN −2 + 2fN −1 + fN ) −
f 00 .
2
12 i=0 i
Handout 6 06/08/02
3
Figure 2: Depiction of how the trapezoidal rule approximates the integral on the subinterval
[xi , xi+1 ].
The error term is given by
h3 00
f0 + f100 + f200 + . . . + fN00 −1 ,
12
!
N h3 f000 + f100 + f200 + . . . + fN00 −1
= −
.
12
N
Error = −
If the mean value of fi00 is given by
f000 + f100 + f200 + . . . + fN00 −1
N
!
,
(9)
then we know that it must lie within the bounds of f 00 (x), and hence it can be represented
as f 00 (ξ) for some ξ such that
00
f (ξ) =
f000 + f100 + f200 + . . . + fN00 −1
N
!
.
(10)
Therefore, since N h = (b − a), the error becomes
Error = −
(b − a)h2 00
f (ξ) = O h2 ,
12
(11)
which shows that the trapezoidal rule is second order accurate.
Simpson’s rules
Simpson’s 1/3 rule
Simpson’s 1/3 rule approximates the function within the interval [xi , xi+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 Simpson’s 1/3 rule approximates the function f (x) with a
quadratic through xi , xi+1 , and xi+2 .
x = xi+1 to obtain
1
1
00
000
0
f (x) = fi+1 + (x − xi+1 )fi+1
+ (x − xi+1 )2 fi+1
+ (x − xi+1 )3 fi+1
2
6
1
4 (iv)
5
+
(x − xi+1 ) fi+1 + O (x − xi+1 ) .
24
The integral in the subinterval [xi , xi+2 ] is then given by
xi+2
Z
f (x) dx =
xi
Z
xi+2
xi
1
1
0
00
000
+ (x − xi+1 )3 fi+1
fi+1 + (x − xi+1 )fi+1
+ (x − xi+1 )2 fi+1
2
6
1
(iv)
(x − xi+1 )4 fi+1 dx ,
+
24
where the truncation error has been left off since the last term will end up being the error.
Making a change of variables such that
s=
2(x − xi+1 )
x − xi+1
=
,
xi+2 − xi
h
(12)
we have
xi+2
Z
f (x) dx = h
Z
+1
−1
xi
1
1
1
(iv)
00
000
0
+ h3 s3 fi+1
+ h4 s4 fi+1 ds ,
fi+1 + hsfi+1
+ h2 s2 fi+1
2
6
24
(13)
which becomes
Z
xi+2
xi
1
1
1
1 5 5 (iv) +1
0
00
000
f (x) dx = hsfi+1 + h2 s2 fi+1
+ h3 s3 fi+1
+ h4 s4 fi+1
+
h s fi+1 ,
2
6
24
120
−1
1 3 00
1 5 (iv)
= 2hfi+1 + h fi+1 + h fi+1 .
3
60
Handout 6 06/08/02
5
Using the second order accurate approximation to the second derivative
00
fi+1
=
fi − 2fi+1 + fi+2 h2 (iv)
− fi+1 ,
h2
12
(14)
the integral becomes
Z
xi+2
xi
1 3 fi − 2fi+1 + fi+2 h2 (iv)
1
(iv)
f (x) dx = 2hfi+1 + h
− fi+1 + h5 fi+1 .
2
3
h
12
60
1
1 5 (iv)
=
h (fi + 4fi+1 + fi+2 ) − h fi+1 .
3
90
!
(15)
The integral over [a, b] is taken by taking the sum of the approximate integrals, as in
Z
b
f (x) dx =
a
=
N/2 Z x
X
i+2
f (x) dx ,
i=0
xi
N/2 1
(iv)
h (fi + 4fi+1 + fi+2 ) − h5 fi+1 .
3
90
X 1
i=0
The sum is given by
1
h(
3
f0
+
fN −6
4f1
+
+
4fN −5
f2
f2
+
+
+ ...
+ fN −4
fN −4
4f3
+
+
+
+
f4
f4
+
+
4fN −3 + fN −2
fN −2
4f5
+
+
+
f6
4fN −1 + fN ) ,
which becomes
Z
b
a
1
(f0 + 4f1 + 2f2 + 4f3 + . . . + 4fN −3 + 2fN −2 + 4fN −1 + fN )
3
X (iv)
1 5 N/2
−
h
f .
90 i=0 i+1
f (x) dx =
The error term is given by
Error = −
X (iv)
1 5 N/2
h
f ,
90 i=0 i+1
(16)
which, using the same arguments as those for the Trapezoidal rule, becomes
Error = −
1
(b − a)h4 f (iv) (ξ) = O h4 ,
180
which shows that Simpson’s 1/3 rule is fourth order accurate.
(17)
Handout 6 06/08/02
6
Simpson’s 3/8 rule
Simpson’s 3/8 rule approximates the function within the subinterval [xi , xi+3 ] using a quartic.
The Taylor series expansion is performed about xi+3/2 to obtain
1
1
0
00
000
+ (x − xi+3/2 )2 fi+3/2
f (x) = fi+3/2 + (x − xi+3/2 )fi+3/2
+ (x − xi+3/2 )3 fi+3/2
2
6
1
+
(x − xi+3/2 )4 f ( iv)i+3/2 + O (x − xi+3/2 )5 .
(18)
24
Integrating this function in a similar manner to that used for the 1/3 rule yields
Z
b
3
h (f0 + 3f1 + 3f2 + 2f3 + 3f4 + 3f5 + . . .
8
+ 2fN −3 + 3fN −2 + 3fN −1 + fN )
1
−
(b − a)h4 f (iv) (ξ) .
80
f (x) dx =
a
(19)
Summary of integration formulas and pseudocodes
Trapezoidal rule
Z
b
a
1
h (f0 + 2f1 + 2f2 + . . . + 2fN −2 + 2fN −1 + fN ) + Error
2
1
Error = − (b − a)h2 f 00 (ξ) = O h2
12
f (x) dx =
1. If fi 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 xi = a + h(i − 1)
Set fi = f (xi )
end
2. Set I = 0
for i = 2 to N
Set I = I + hfi
end
Set I = I + 21 h(f1 + fN +1 )
3. The integral is given by I.
Handout 6 06/08/02
Simpson’s 1/3 rule (N divisible by 2)
Z
b
a
1
(f0 + 4f1 + 2f2 + 4f3 + . . . + 4fN −3 + 2fN −2 + 4fN −1 + fN )
3
+ Error
1
Error = −
(b − a)h4 f (iv) (ξ) = O h4
180
f (x) dx =
1. If fi 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 xi = a + h(i − 1)
Set fi = f (xi )
end
2. Set I = 0
for i = 1 to N2
Set I = I + 43 hf2i
end
for i = 1 to N2 − 1
Set I = I + 23 hf2i+1
end
Set I = I + 13 h(f1 + fN +1 )
3. The integral is given by I.
7
Handout 6 06/08/02
8
Simpson’s 3/8 rule (N divisible by 3)
Z
b
a
3
h (f0 + 3f1 + 3f2 + 2f3 + 3f4 + 3f5 + . . .
8
+ 2fN −3 + 3fN −2 + 3fN −1 + fN )
+ Error
1
Error = − (b − a)h4 f (iv) (ξ) .
80
f (x) dx =
(20)
1. If fi 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 xi = a + h(i − 1)
Set fi = f (xi )
end
2. Set I = 0
for i = 2 to N
Set I = I + 89 hfi
end
for i = 1 to N3 − 1
Set I = I − 83 hf3i+1
end
Set I = I + 38 h(f1 + fN +1 )
3. The integral is given by I.
Download