You are on page 1of 7

Numerical Integration

1. General considerations
2. Trapezoid rule
f(x)

3. Simpsons rule
4. Error estimates
5. Gaussian quadrature
b

Consider the integral

I f ( x) dx
a

The general strategy for estimating I is to


evaluate f(x) at a number of points and
approximate I by a sum
where the wi are weight factors.

a x1 x2 xN 1 xN b
b

i 1

f i f ( xi )

I f ( x)dx f i wi

Criteria for the choice of a method are the desired accuracy, the efficiency of the method
and particular properties of the integrand (for example singularities).

Trapezoid rule

f(x)

N=5, n=4

N evenly spaced points xi


n=N-1 intervals of size h
in each interval, approximate the
curve f(x) by a straight line (Taylor
expansion to first order)
ba
h
N 1
xi a (i 1)h, i 1, , N

T1
a=x1

T2
x2

T3
x3

T4
x4

b=x5

1
h
Ti ( xi 1 xi ) ( f i 1 f i ) ( f i 1 f i ) area of the trapezoid
2
2
h
h
h
h
I IT T1 T2 Tn ( f1 f 2 ) ( f 2 f 3 ) ( f N 2 f N 1 ) ( f N 1 f N )
2
2
2
2
N 1
h
h
h
f1 h f 2 h f N 1
f N ( f1 f N ) h f i
2
i 2
w2
wN 1
2
2
w1
wN
N

h

h
IT wi f i w f with w , h, h, , h, and f f1 , f 2 , , f N
2
2
i 1

f(x)

Simpsons rule

N=5

N evenly spaced points xi


n=N-1 intervals of size h
in each pair of intervals, approximate
the curve f(x) by a parabola (Taylor
expansion to second order)

a=x1

1
f ( x) f ( xi ) f ( xi )( x xi ) f ( xi )( x xi ) 2


2
y
y2

xi 1

xi 1

x2
for

x3

x4

b=x5

h
xi 1 x xi 1 h y h
h

1
1
1
1
h

f ( x) dx dy f ( xi ) f ( xi ) y f ( xi ) y 2 f ( xi ) y h f ( xi ) y 2 f ( xi ) y 3
2
2 h 2
3 h

h
1
2hf ( xi ) h3 f ( xi )
3
but

1
h
h
1
f ( xi ) ( f ( xi ) f ( xi )) 2 ([ f ( xi h) f ( xi )] [ f ( xi ) f ( xi h)])
h
2
2
h
1
2 ( f i 1 2 f i f i 1 )
h

xi 1

1 3 1
1
4
1
f
(
x
)
dx

2
hf

h
(
f

2
f

f
)

hf

hf

hf i
i
i 1
i
i 1
i 1
i
2
x
3 h
3
3
3
i 1

n
pairs of intervals to obtain the value for the integral from a to b
2
b
1
4
1
1
4
1
1
4
1
I f ( x)dx I S hf1 hf 2 hf 3 hf 3 hf 4 hf 5 hf N 2 hf N 1 hf N
3
3
3
3
3
3
3
a
3 3
2hf
3
3
N

4h h

h 4h 2h 4h 2h
I S wi f i w f with w , , , , , , , and f f1 , f 2 , , f N
3 3
3 3 3 3 3
i 1
add the contributions of the

The Simpson rule approximation IS to the integral I is called Sn in Cooper, A Matlab


Companion , pp 172-173
The number of intervals (n = N-1) has to be even!
The sum over the weights provides a useful check of the derivation
N

w ( N 1)h nh (b a)
i 1

Error estimates
Consider the Taylor expansion of f ( x) about xi ; as before, write y x xi
1
1
1
f ( xi ) y 2 f ( xi ) y 3 f ( 4 ) ( xi ) y 4
2
3!
4!
For Simpson' s rule, we have considered terms up to order y 2 . Let us now estimate the error
ES in the integral value I S due to the third and fourth order terms. We start by estimating
f ( x) f ( xi ) f ( xi ) y

the error S in the integral over a single pair of intervals : xi 1 x xi 1 h y h


h

1
1
1 ( 4)
1
1
S dy f ( xi ) y 3 f ( 4 ) ( xi ) y 4 f ( xi ) y 3dy f ( 4 ) ( xi ) y 4 dy
f ( xi )h5
4!
4!
60
3!
3!
h
h
h

0
2 h5
5
Note: the value of the prefactor for S is incorrect because we have also approximated the
second derivative of f (x) when we derived Simpsons rule. A more careful analysis yields:
1
S f ( 4 ) ( )h5 for some such that xi 1 xi 1
90
(b a )
1 (b a ) ( 4 )
n
Using h
we find S
f ( )h 4 and for ES S
n
90 n
2
1 (4)
1 (4)
(b a )5
4
ES
f ( )(b a )h
f ( )
for some such that a b
180
180
n4

Even though we have used second order polynomials to approximate the function over
the pairs of intervals, the Simpson rule yields exact results for polynomials up to third
order, since the error is proportional to a fourth-order derivative.
ES 1/n4 doubling the number of intervals reduces the error by a factor 16 .
The error3 estimates for the Trapezoid rule are derived in a similar way and given by
h
T f ( ) for some such that xi xi 1
12
1
1
(b a )3
2
ET (b a ) f ( )h f ( )
for some such that a b
2
12
12
n
doubling the number of intervals reduces the error by a factor 4
Matlab offers a numerical integration routine quad that uses an adaptive Simpson
quadrature. Quadruature is the general term for numerical integration methods (from
counting squares under a curve). An adaptive integration method uses different interval
sizes depending on the behavior of the function and the desired tolerance.
Assume we want to evaluate the integral between two points u and v with au<vb:
1. Split [u,v] into two intervals of size h and use Simpsons rule, call the result S(2)
2. Split [u,v] into four intervals of size h/2 and use Simpsons rule, call the result S(4)
3. Calculate the error estimate (2) | S(4)- S(2)|
4. If the error is smaller than the tolerance, accept the result and go on to the next
interval [v,w]. Otherwise, split [u,v] into 8 intervals of size h/4 , etc.

Gaussian quadrature
Please read the handouts from: A. L. Garcia, Numerical Methods for Physics, pp 325328, and Landau and Paez, Computational Physics, pp 55-57
Remember the general form of numerical integration:
b

I f ( x)dx f i wi
a

i 1

with a x1 x2 xN 1 xN b and

f i f ( xi )

Gaussian quadratures use evaluation points xi (not evenly spaced) and weights wi that
yield an optimum approximation to the integral for specific types of functions.
Gaussian quadrature is the preferred method if you can choose a type (Legendre,
Laguerre, Chebyshev, etc. ) appropriate for your problem. In that case, you can achieve
much higher accuracy with many fewer evaluation points compared to the other
integration methods.
However, in general, it is difficult to estimate the error and there is no guarantee that the
result is a good approximation to the integral.
Matlab offers a routine quadl that employs a variant of Gaussian quadrature. It may
work very well, but be careful when using it and check your results with a different
method from time to time.

You might also like