Computational Mathematics/Information Technology

Numerical Integration
Computational Mathematics/Information
Technology
Dr Oliver Kerr
2009–10
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Introduction
In many problems we may want to find integrals like
Z
x=b
f (x) dx
x=a
where we know or can calculate f (x), but cannot do the
integration explicitly.
What can we do?
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Introduction
In many problems we may want to find integrals like
Z
x=b
f (x) dx
x=a
where we know or can calculate f (x), but cannot do the
integration explicitly.
What can we do?
We have already seen one thing in the work sheets — integrate a
spline.
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Trapezium rule
f(x)
x0
x1
x2
x3
x4
x
The area under the first line segment is
Z
x1
f (x0 ) + (x − x0 )
x0
(f (x1 ) − f (x0 ))
f (x0 ) + f (x1 )
dx =
(x1 − x0 )
(x1 − x0 )
2
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Apply this to our integral
Z
x=b
f (x) dx
x=a
Let us divide the interval into N intervals. There will be a gap of
h = (b − a)/N
between our xs. So
x0 = a,
x1 = a + h,
x2 = a + 2h, . . . ,
Dr Oliver Kerr
xN = a + Nh = b
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Then our guess at the integral will be the Trapezium rule:
Z
x=b
f (x) dx ≈
x=a
=
N−1
X
h(f (a + nh) + f (a + (n + 1)h)/2
n=0
h
(f (a) + 2f (a + h) + 2f (a + 2h) + · · · + 2f (b − h) + f (b))
2
How accurate is this?
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Then our guess at the integral will be the Trapezium rule:
Z
x=b
f (x) dx ≈
x=a
=
N−1
X
h(f (a + nh) + f (a + (n + 1)h)/2
n=0
h
(f (a) + 2f (a + h) + 2f (a + 2h) + · · · + 2f (b − h) + f (b))
2
How accurate is this?
It will depend on N.
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
We will look at an example where we can do the integration. We
will integrate
f (x) = sec2 (x)
between x = 0 and x = 1
Z 1
sec2 x dx = tan 1 ≈ 1.557407725
0
We will see what happens as we increase N
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
N = 1,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 2.21275949,
Dr Oliver Kerr
Error= 0.655351758
Computational Mathematics/Information Technology
Numerical Integration
N = 2,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.75560296,
Dr Oliver Kerr
Error= 0.198195219
Computational Mathematics/Information Technology
Numerical Integration
N = 4,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.61106944,
Dr Oliver Kerr
Error= 0.0536617041
Computational Mathematics/Information Technology
Numerical Integration
N = 8,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.57117105,
Dr Oliver Kerr
Error= 0.0137633085
Computational Mathematics/Information Technology
Numerical Integration
N = 16,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.56087255,
Dr Oliver Kerr
Error= 0.003464818
Computational Mathematics/Information Technology
Numerical Integration
N = 32,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.55827546,
Dr Oliver Kerr
Error= 0.000867724419
Computational Mathematics/Information Technology
Numerical Integration
N = 64,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.55762482,
Dr Oliver Kerr
Error= 0.000217080116
Computational Mathematics/Information Technology
Numerical Integration
N = 128,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.5574621,
Dr Oliver Kerr
Error= 0.000054359436
Computational Mathematics/Information Technology
Numerical Integration
N = 256,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.5574218,
Dr Oliver Kerr
Error= 0.0000140666962
Computational Mathematics/Information Technology
Numerical Integration
N = 512,
Introduction
Trapezium Rule
Simpson’s rule
Estimate= 1.55741155,
Dr Oliver Kerr
Error= 0.00000381469727
Computational Mathematics/Information Technology
Numerical Integration
N
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
Estimate
2.21275949
1.75560296
1.61106944
1.57117105
1.56087255
1.55827546
1.55762482
1.55746210
1.55742180
1.55741155
1.55740809
1.55740845
1.55740631
1.55740643
Introduction
Trapezium Rule
Simpson’s rule
Error
0.655351758
0.198195219
0.0536617041
0.0137633085
0.003464818
0.000867724419
0.000217080116
5.4359436E-05
1.40666962E-05
3.81469727E-06
3.57627869E-07
7.15255737E-07
-1.43051147E-06
-1.31130219E-06
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
The error is roughly reduced by a factor of 4 each time we double
N.
Two questions:
1. Is this to be expected?
2. Can we do better?
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
To find the estimate of the error we look at the error in one
segment from a to a + h.
We will use the result
f (a + x) = f (a) + xf 0 (a) + x 2 f 00 (a + θ(x)x)/2
for some 0 < θ(x) < 1. This means the second derivative is
evaluated somewhere between a and a + x.
Dr Oliver Kerr
Computational Mathematics/Information Technology
Introduction
Trapezium Rule
Simpson’s rule
Numerical Integration
If we integrate
f (a + x) = f (a) + xf 0 (a) + x 2 f 00 (a + θ(x)x)/2
for x between 0 and h we get
Z
h
Z
f (a + x) dx =
0
h
0
Z
f (a) + xf (a) dx +
0
h
x 2 f 00 (a + θ(x)x)/2 dx
0
We need an estimate of f 0 (a), but we just use our result again.
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
f (a + h) = f (a) + hf 0 (a) + h2 f 00 (a + θ(h)h)/2
so
f 0 (a) =
Hence
Z
h
f (a + h) − f (a)
− hf 00 (a + θ(h)h)/2
h
f (a) + xf 0 (a) dx = hf (a) +
0
h2
= hf (a) +
2
=h
h2 0
f (a)
2
f (a + h) − f (a)
− hf 00 (a + θ(h)h)/2
h
f (a) + f (a + h) h3 00
− f (a + θ(h)h)
2
4
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
We are left with
Z
h
x 2 f 00 (a + θ(x)x)/2 dx
0
which is an integral we cannot do — but we can make an estimate.
If
00 f (x) < M
then
Z
0
h
for all
Z
x f (a + θ(x)x)/2 dx ≤
2 00
h
0≤x ≤h
x 2 f 00 (a + θ(x)x)/2 dx
0
Z
<
h
x 2 M/2 dx =
0
Dr Oliver Kerr
h3
M
6
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Combining these results gives
Z
h
f (a + x) dx = h
0
f (a) + f (a + h)
+ (various terms) × h3
2
If we add N terms like this then the error will be the sum of N
terms all proportional to h3 . But h = (b − a)/N so
Z
x=b
f (x) dx =
x=a
h
(f (a)+2f (a+h)+2f (a+2h)+· · ·+2f (b−h)+f (b))
2
+(various terms) × h2
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Simpson’s rule
How can we do better?
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Simpson’s rule
How can we do better?
Let
In =
h
(f (a) + 2f (a + h) + 2f (a + 2h) + · · · + 2f (b − h) + f (b)),
2
where h = (b − a)/N.
Then
Z
x=b
f (x) dx = IN + A/N 2 + smaller terms
x=a
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Introduction
Trapezium Rule
Simpson’s rule
Simpson’s rule
How can we do better?
Let
In =
h
(f (a) + 2f (a + h) + 2f (a + 2h) + · · · + 2f (b − h) + f (b)),
2
where h = (b − a)/N.
Then
Z
x=b
f (x) dx = IN + A/N 2 + smaller terms
x=a
Z
x=b
f (x) dx = I2N + A/(2N)2 + smaller terms
x=a
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Z
Introduction
Trapezium Rule
Simpson’s rule
x=b
f (x) dx = IN + A/N 2 + smaller terms
x=a
Z
x=b
f (x) dx = I2N + A/(2N)2 + smaller terms
x=a
Take 4 times the bottom equation and subtract off the top one to
get rid of the A terms:
Z
x=b
f (x) dx = 4I2N − IN + smaller terms
3
x=a
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
Z
Introduction
Trapezium Rule
Simpson’s rule
x=b
f (x) dx = IN + A/N 2 + smaller terms
x=a
Z
x=b
f (x) dx = I2N + A/(2N)2 + smaller terms
x=a
Take 4 times the bottom equation and subtract off the top one to
get rid of the A terms:
Z
x=b
f (x) dx = 4I2N − IN + smaller terms
3
x=a
This should be more accurate as the 1/N 2 error terms have gone.
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
We now have
Z
Introduction
Trapezium Rule
Simpson’s rule
x=b
f (x) dx ≈ 4I2N − IN
3
x=a
This can be written as
Z
x=b
f (x) dx
x=a
=
h
(f (a) + 4f (a + h) + 2f (a + 2h) + 4f (a + 3h) + 2f (a + 4h) + · · ·
3
· · · + 2f (b − 2h) + 4f (b − h) + f (b))
This is Simpson’s rule.
Dr Oliver Kerr
Computational Mathematics/Information Technology
Numerical Integration
N
2
4
8
16
32
64
128
256
512
1024
Estimate
1.60321748
1.56289148
1.55787134
1.55743992
1.55740976
1.55740821
1.55740786
1.55740786
1.55740798
1.55740702
Introduction
Trapezium Rule
Simpson’s rule
Error
0.0458097458
0.00548374653
0.000463604927
3.21865082E-05
2.02655792E-06
4.76837158E-07
1.1920929E-07
1.1920929E-07
2.38418579E-07
-7.15255737E-07
Dr Oliver Kerr
Computational Mathematics/Information Technology