Vous êtes sur la page 1sur 18

Chapter 6

Quadrature
In this chapter well look at:
Different techniques for numerically approximating denite

integrals
Matlab for implementing these
Giving bounds on the error in the approximations.

Chapter 6
Quadrature
The failure time for a component is follows a normal
distribution with an average failure time of 3 years and a
standard deviation of 0.8 years. You want to know what
proportion of these components fails within the warranty period
of 1 year.
Youll see that this amounts to nding the value of the integral
 1
5
2
e25(x1) /32 dx.
P=
4 2
5(x 1)
then this becomes
If you let t =
4
2.5
1
2
et /2 dt.
P=
2
2
But you dont know an antiderivative for et /2 ???

Quadrature
In other settings, you may not even have a formula for the
function you want to integrate.

Example
Sydney Water can tell how fast water is owing out of
Warragamba Dam at time t. If F (t) is the ow rate at time t
then the volume
 t2 of water used between time t1 an t2 is
V = t1 F (t) dt.
In practice there will not be a formula for F (t), although you can
obtain lots of values of the function.
In both examples, to calculate the integral youll need to resort
to numerical methods. The technical term for numerical
integration is quadrature.

Riemann sums
Our denition of the Riemann integral tells us that we can
 b
f (x) dx as closely as we like by lower (or
approximate
a

upper) Riemann sums S P = nk =1 (ak ak 1 )f k .
y
f (x)

a0

a1

b = an x

Problem: In practice, nding the heights of the largest


rectangles under the graph may be complicated!

Riemann sums
If f is continuous, then the heights of these rectangles are
actually equal to f (xk ) for some xk in [ak 1 , ak ].

fk

ak 1

xk

so the Riemann sum is of the form

ak
n


f (xk )wk where

k =1

wk = (ak ak 1 ).

General quadrature
But:
In practice, nding the minimum points xk is hard
Youd need to calculate an upper and lower Riemann sum

to know how accurate your estimate was


 b
f (x) dx S P .
SP
a

Good news: Under certain assumptions on f , you can estimate


 b
n

f (x) dx by weighted sums of the form
f (xk )wk where
a

k =1

there is a simple rule for choosing the points xk and a simple


formula for the weights wk .
These rules come from approximating the function f with nice
functions which we can integrate analytically.

Quadrature rules
Denition 

Let I(f ) =

f (x)dx.

A quadrature rule is an approximation to the exact integral I(f )


which is of the form
n

f (xk )wk ,
Q(f ) =
where

k =1

wk are called quadrature weights and


xk are called quadrature points which satisfy

a x1 < x2 < < xn b.

The critical question is how to choose the weights wi and the


quadrature points xi to achieve a desired accuracy with the
minimum computational cost.

Rule 1: The trapezoidal rule

ba
Subdivide [a, b] into n subintervals of equal length h =
.
n
Let xk = a + kh, j = 0, . . . , n.
On each subinterval, join the endpoints of the graph to form a
trapezoid with area
(f (xk 1 ) + f (xk ))
.
Ak = h
2
f (xk 1 )
f (xk )

xk 1

xk

Trapezoidal rule
Denition (Trapezoidal rule)
The trapezoidal rule is given by


n
n


h
f (x0 ) + 2
Ak =
f (xk ) + f (xn ) .
Tn =
2
k =1

a = x0

k =1

x1

x2

x3 . . . xn1

xn = b

Trapezoidal rule
Example

1
dx by the trapezoidal rule.
x
1
(Exact value: n 2 = 0.693147 . . .)
1
5 6 7
21
= , P = {1, , , , 2},
n = 4 = h =
4
4
4 4 4

8 4 8 1
1
1+ + + +
= 0.69702381
= T4 =
8
5 3 7 2
= error = E4 = I T4 = 0.00387663
1
9 5 11 3 13 7 15
n = 8 = h = , P = {1, , , , , , , , 2}
8
8 4 8 2 8 4 8
= T8 = 0.69412185 = I T8 = 0.00097467
Approximate I =

n = 16 = T16 = 0.69339120 = I T16 = 0.00024402.


The errors decrease by a factor of about 4 each time we
double n. This suggests that the error En = I Tn n2 or h2 .

Trapezoidal rule
Example
Use the Matlab function trapz to approximate the integral
 1
cos x
dx.
I=
x
1 1 + e
>> f = @(x) cos(x) ./ ( 1 + exp(x) ); % note dot
>> n = 4;
>> x = linspace(-1,1,n+1); % divide [-1,1]
% by n+1 points
>> y = f(x); % a vector of the y values
>> Tn = trapz(x,y)
Tn =
0.8239
>> % Another way to use trapz
>> h = x(2)-x(1);
>> Tn2 = h * trapz(y)
Tn2 = 0.8239

Midpoint rule
Perhaps a simpler idea would be just to approximate the
integral by taking the height at the midpoint of each interval.
f (xk 1 )
f (mk )

xk 1

mk

f (xk )

xk

Question: Is this better or worse than the trapezoid rule?

Midpoint rule
Denition
Let h = (b a)/n and let mk = a + (k 12 )h for 1 j n.
 b
f (x) dx is given by
The midpoint rule approximation to I =
a

Mn = h (f (m1 ) + f (m2 ) + + f (mn )) .

a = x0

x1

x2

x3 . . . xn1

xn = b

Midpoint rule
Example

Approximate I =
1

1
dx by the midpoint rule.
x

9 11 13 15
n = 4 = mid points are , , ,
8 8 8 8

1 8
8
8
8
= 0.69121989
= M4 =
+
+
+
4 9 11 13 15
= E4 = 0.00192729
n = 8 = M8 = 0.69266055 = E8 = 0.00048663
n = 16 = M16 = 0.69302521 = E16 = 0.00012197.
Similarly to the trapezoidal rule, En n12 , but they are now
positive and about half the size of those for the trapezoidal rule!

A Matlab code for the midpoint rule

function Mn = midpoint(f,a,b,n)
% integrates the function f from a to b
% by using the midpoint rule with n midpoints,
% i.e., n subintervals.
h = (b-a)/n;
x = [a:h:b];
m = (x(2:end) + x(1:end-1)) / 2;
Mn = sum(h .* f(m));

Simpsons rule
The trapezoidal rule and the midpoint rule approximate the
function f by linear functions over each interval [xk 1 , xk ].
You might hope to do better by approximating f by a quadratic
(or higher order) polynomial on each interval.
There is exactly one quadratic p which agrees with f at the
endpoints xk 1 and xk , and the midpoint mk :
(xmk )(xxk )
p(x) = f (xk 1 ) (xk 1
mk )(xk 1 xk )
(xx

)(xx )

(xx

)(xm )

k
k 1
k
+f (mk ) (mk xkk 1
+
f
(x
)
.
k
)(m
x
)
(x
x
)(x
m
1
k
k
k
k 1
k
k)
Integrating this quadratic is easy (but messy) and gives the
formula  x
k
xk xk 1
p(x) dx =
(f (xk 1 ) + 4f (mk ) + f (xk )).
6
xk 1
Important point: You dont need to nd p as you are only
interested in the RHS!

Simpsons rule
Example: x0 = 0, x1 = 2, m1 = 1
f (x0 ) = 3, f (x1 ) = 2, f (m1 ) = 2
give p(x) = 12 x 2 32 x + 3
f (xk 1 )
f (mk )

xk 1


x1

so

f (x) dx

x0

...=

mk

f (xk )

xk

1 2 3
13
x x + 3 dx =
2
2
3

2
(f (x0 ) + 4f (m1 ) + f (x1 ))
6

Simpsons rule
If you add up all the areas now you get that
 b
h

f (x) dx
(f (x0 ) + 4f (m1 ) + f (x1 ))
6
a
+(f (x1 ) + 4f (m2 ) + f (x2 )) + (f (x2 ) + 4f (m
3 ) + f (x3 ))
+ + (f (xn1 ) + 4f (mn ) + f (xn ))
h
= (f (x0 ) + 4f (m1 ) + 2f (x1 ) + 4f (m2 ) + 2f (x2 )
6
+ + 2f (xn1 ) + 4f (mn ) + f (xn )).
Notation: Let yk = f (xk ),

for 0 k n.
x
+x
yk = f (mk ) = f k 12 k , for 1 k n.
Simpsons

rule with n intervals of length h is then
h
Sn = 6 y0 + yn + 4(y1 + + yn ) + 2(y1 + + yn1 ) .

Relation between different rules

With this notation then

h
[y0 + 2(y1 + + yn1 ) + yn ]
2
Mn = h (y1 + y2 + + yn )
h
Sn = [y0 + yn + 4(y1 + + yn ) + 2(y1 + + yn1 )] .
6
Tn =

Hence, Simpsons rule is a weighted average of the

trapezoidal and midpoint rules:


Sn =

1
2
Tn + Mn .
3
3

A Matlab code for Simpsons rule

function [S, M, T] = simpson(f, x)


% [S, M, T] = simpson(f, x)
% applies the Simpson, midpoint
% and trapezoidal rules
% to f(x) using the partition x.
h = diff(x);
xhalf = x(1:end-1) + h/2;
M = sum(h .* f(xhalf));
T = sum((h/2) .* ( f(x(1:end-1)) + f(x(2:end)) )
S = ( 2 * M + T ) / 3;

Errors
Theorem
Suppose that f has a continuous second order derivative which
satises |f  (x)| K on [a, b]. Then
K (b a)3
K (b a) 2
|I(f ) Tn |
=
h
12
12n2
K (b a)3
K (b a) 2
|I(f ) Mn |
=
h .
24
24n2

a = x0

x1

x2

x3 . . . xn1

xn = b

Trapezoidal rule error bound


Example
Obtain bounds for the errors for T4 , T8 , M4 and M8 for
 2
1
dx
I=
1 x
If f (x) = 1/x then f  (x) = 2/x 3 so that |f  (x)| 2 on [1, 2].
Hence we may take K = 2 in the estimates

2(2 1)3 1 2
= 0.0104
|I T4 |
12
4

2(2 1)3 1 2
= 0.0052
|I M4 |
24
4

2(2 1)3 1 2
= 0.0026
|I T8 |
12
8

2(2 1)3 1 2
= 0.0013.
|I M8 |
24
8
The actual errors are considerably smaller.

Comment regarding error terms


Big-O notation
We say f (n) = O(g(n)) as n if there is a positive M such
that
|f (n)| M|g(n)|
for large enough n.
This gives a useful way to express how fast the approximations
converge to the actual integral as n increases.
In big-O notation

1
|I Tn | = O
n2


and

|I Mn | = O

1
n2


as

n .

Comment regarding error terms


What is we only have numerical values for f (x), but not a
formula?
In this case we cannot explicitly determine K = max |f  (x)|.
However, we can still assume
Midpoint rule error Trapezoidal rule error;
Errors are O(1/n2 ).

Error estimate for Simpsons Rule


Theorem
Suppose that f has a continuous fourth order derivative
satisfying |f (4) (x)| L on [a, b]. Then
L(b a) 4 L(b a)5
.
|I(f ) Sn |
h =
2880
2880n4

Notes
ba
.
n
Many books quote
4
|I(f ) Sn | L(ba)
180 h
because they integrate over [x0 , x2 ], [x2 , x4 ], . . . , and
ba
h=
. Note that 2880 = 24 180.
2n
Recall that h =

Error estimate for Simpsons Rule


Using big-O notation we have


b
a


f (x) dx = Sn + O

1
n4


as n .

Example
Estimate n if we wish to use Simpsons rule to approximate
 2
dx
to within 106 .
1 x

Matlab script for Simpons rule




This Matlab script computes


1

dx
to within 106 .
x

% Matlab script to compute


% \int_a^b dx/x by Simpsons rule
% to within 1e-6.
format long
format compact
f
a
x
S
I
E

=
=
=
=
=
=

@(x) 1./x;
1; b = 2; n = 10;
linspace(a,b,n+1);
simpson(f,x)
log(2)
% exact value of integral
I-S

Proof of the error estimate for Tn


Consider rst the error in the subinterval [x0 , x1 ], i.e.,
x1
f (x0 ) + f (x1 )
E1 =
f (x) dx h
2
x0
where h = ba
n = x1 x0 .
Let y0 = f (x0 ) and y1 = f (x1 ).
Then the equation of the line passing through (x0 , y0 ) and
(x1 , y1 ) is
y = y0 + m(x x0 )
y1 y0
where m =
.
x1 x0
Let g(x) = f (x) [y0 + m(x x0 )].
Then  x
 x
1

x0

g(x) dx =

x0

f (x) dx Area trapezoid = E1 .

Note that g(x0 ) = g(x1 ) = 0 and g  (x) = f  (x).

Proof (continued)
We
by parts twice:
 xintegration
 x1 now work backwards and use
1
(x x0 )(x1 x)f  (x) dx =
(x x0 )(x1 x)g  (x) dx
x0

x0

= (x x0 )(x1


x)g  (x)

x1
x0

x1

v

(2x + x0 + x1 )g  (x) dx

x0

x1

(2x x0 x1 )g  (x) dx
x
 0
x1  x1
= (2x x0 x1 )g(x)
2g(x) dx
x0
x0
 x1
=
2g(x) dx.
=

Thus

x0



|E1 | = 

x1
x0

  
 1
g(x) dx  = 
2

x1
x0




(x x0 )(x1 x)f (x) dx .

Proof (continued)
  x

1
1

|E1 | = 
(x x0 )(x1 x)f  (x) dx 
2 x0

1 x1

|(x x0 )(x1 x)| |f  (x)| dx


2 x0

K x1

[x 2 + (x0 + x1 )x x0 x1 ] dx
2 x0
K
K 3
=
(x1 x0 )3 =
h .
12
12
The same estimate holds for each of the n = (b a)/h
intervals, so the total error is
K (b a)3
K 3 K (b a)h2
.
|I(f ) Tn | n h =
=
12
12
12n2

Hence

Back to the Normal Distribution


We started this chapter with a problem that came down to
estimating the improper integral
 2.5
1
2

et /2 dt.
2
We can turn this into nding a proper integral by using the fact
(from Probability) that
 0
1
1
2

et /2 dt = .
2
2
and so
 2.5
 0
1
1
1
2
2

et /2 dt =
et /2 dt.
2
2
2 2.5
To get a bound on our error we need to bound derivatives of
2
f (t) = 1 et /2 on [2.5, 0].
2

Back to the Normal Distribution


Some tedious differentiation shows that if f (t) =

2 /2  4
1
(4)
t
2

f (t) =
e
t 6t + 3 .
2
How big can that get on [2.5, 0]?

2
1 et /2
2

In practice, youd probably just plot f (4) (t) and read off a
suitable upper bound from the graph |f (4) (t)| 1.2 = L.

then

Back to the Normal Distribution


If we split [2.5, 0] into n imtervals then we have that
L(b a)5
0.041
|I(f ) Sn |

.
2880n4
n4
If we want the error smaller than 109 then
0.041
< 109 n4 > 0.041 109 = 4.1 107
4
n
n > 80.02.
. . . so taking n = 100 should be more than enough!
But: The above error estimate assumes that we can calculate
Sn exactly; or at least to high accuracy.
If you have an error of size 109 in each evaluation of yk and
yk , the total error in calculating S100 might be about
200 109 = 2 107
which is much bigger than the quadrature error.

Quadrature and evaluation errors


For our example, using 10 digit computer accuracy
n
8
32
64
128
512
32,768
1,048,576

Sn
0.4937898595
0.4937903330
0.4937903345
0.4937903343
0.4937903347
0.4937903388
0.4937916453

error
5 107
2 109
2 1010
4 1010
3 1011
4 109
2 106

If you have 20 digit machine accuracy, the error in S128 drops to


about 1011 in line with our analysis.
Moral: Clever quadrature and/or lots of intervals wont
overcome imprecise data!

Interpreting the error


Assuming that your computer has suitable accuracy (eg
Matlab), youll nd that
S100 = 0.4937903346549063 . . . .
The error formula tells us that
L(b a)5
1.2 2.55
=
< 5 1010 = .
|I(f ) S100 |
4
8
2880n
2880 10
What this tells us is that for certain
S100  < I(f ) < S100 + 
or
0.49379033415 < I(f ) < 0.49379033515.
Returning to the original problem
 2.5
1
2

et /2 dt = 0.5 I(f )
2
= 0.00620966534 5 1010 .

Vous aimerez peut-être aussi