You are on page 1of 9

ECM6 Computational Methods :

Slide 1 of 5

Lecture 14b
Numerical Quadrature
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam

ECM6Lecture14bVietnam_2014.nb

Quadrature Rules
n

xN

f HxL x = Wi f Hxi L

x0

i=0

Here Wi are called the quadrature weights. The simplest algorithm is to have equally spaced points and
use a Lagrange Interpolation Polynomial through the points, In this case the quadrature rule is called
Newton-Cotes.
Another approach is for a given number of data points, we select weights and abscissa such that the
quadrature rule gives high accuracy. This is called Gaussian Quadrature.

Booles Rule
With this quadrature rule, we have 5 interpolation points Hx0 , x1 , x2 , x3 , x4 < a distance Dx apart.
To get our quadrature rule we simply integrate
5

x4

f HxL x Wi f Hxi L

x0

i=1

where the weights Wi are defined as


x4

Wi =

Li HxL

x0

Here
5

Li =
k=0

x - xk
xi - xk

ki

is the Lagrange interpolating polynomial. Thus for our problem if i=2 we get

L2 =

Hx - x0 L Hx - x1 L Hx - x3 L Hx - x4 L
Hx2 - x0 L Hx2 - x1 L Hx2 - x4 L Hx2 - x4 L

Mathematica Implementation
In this example we show how to implement the Boole quadrature rule, using Mathematica.
We have 5 interpolation points a distance Dx apart. The Lagrange interpolation formula is

ECM6Lecture14bVietnam_2014.nb

In[100]:=

f2 =

Hx - x1L Hx - x2L Hx - x3L Hx - x4L

f@x0D +
Hx0 - x1L Hx0 - x2L Hx0 - x3L Hx0 - x4L
Hx - x0L Hx - x2L Hx - x3L Hx - x4L
f@x1D +
Hx1 - x0L Hx1 - x2L Hx1 - x3L Hx1 - x4L
Hx - x0L Hx - x1L Hx - x3L Hx - x4L
f@x2D +
Hx2 - x0L Hx2 - x1L Hx2 - x3L Hx2 - x4L
Hx - x0L Hx - x1L Hx - x2L Hx - x4L
f@x3D +
Hx3 - x0L Hx3 - x1L Hx3 - x2L Hx3 - x4L
Hx - x0L Hx - x1L Hx - x2L Hx - x3L
f@x4D;
Hx4 - x0L Hx4 - x1L Hx4 - x2L Hx4 - x3L

Integrating and then simplifying using appropriate transformations gives the Boole quadrature rule
In[101]:=

Integrate@f2, 8x, x0, x4<D . x1 x0 + Dx . x2 x0 + 2 Dx . x3 x0 + 3 Dx .


x4 x0 + 4 Dx Simplify
2

Out[101]=

45

Dx H7 f@x0D + 32 f@x0 + DxD + 12 f@x0 + 2 DxD + 32 f@x0 + 3 DxD + 7 f@x0 + 4 DxDL

This formula can be written as

4 Dx
90
H7 f@x0 D + 32 f@x0 + DxD + 12 f@x0 + 2 DxD + 32 f@x0 + 3 DxD + 7 f@x4 DL
Let us implement this formula in Mathematica
NewtonCotes@f_, deltaX_, x0_D := ModuleB8Dx = deltaX<,
4 Dx
90

H7 f@x0D + 32 f@x0 + DxD + 12 f@x0 + 2 DxD + 32 f@x0 + 3 DxD + 7 f@x0 + 4 DxDLF

Now let us evaluate


p

Sin@xD x = 2

p4

Our formula gives a reasonably accurate result with 5 nodes with Dx = Hp - p 4L 4 = 0.589049
NewtonCotes@Sin, 0.589049, p 4D
1.70692

The exact value for the above integral is 1 +


Integrate@Sin@xD, 8x, p 4, p<D
1+

1
2

1
2

=1.70711

ECM6Lecture14bVietnam_2014.nb

Quadrature Rule for Fixed Domains


Sometimes it is convenient to always work with a quadrature rule defined over a fixed interval say
-1 < x < 1. Thus if we have 5 nodes equally spaced then Dx=1/2, then our quadrature rule becomes
1

f HxL x =

7 f@-1D + 32 fB-

45

-1

1
F + 12 f@0D + 32 fB F + 7 f@1D
2
2

Suppose then if we have a integral defined on the interval c < t < d and we want to evaluate it on the
interval a < x < b
d

f HtL t f HxL x
c

Transforming Integration Domain


We want to find the domain such that

t=c x=a
t=d x=b
Thus we need the transformation between the two systems. We want to find the relation

Vx=t-Q
such that
x=at=c
x=bt=d
Solving at the two limits gives

V a = c - Q, V b = d - Q
Solving these equations gives
Solve@8V a == c - Q, V b == d - Q<, 8V, Q<D
::V -

-c + d
a-b

,Q-

bc-ad
a-b

>>

Thus we have

V=

d-c
b-a

, Q=

b c - ad
b-a

Now if we take a = -1, b = 1, then


V=

d-c
2

, Q=

c+d
2

This means that


d

f HtL t =
c

d-c
b-a

f HV x + QL t
a

ECM6Lecture14bVietnam_2014.nb

d-c
2

d-c

-1

x+

c+d
2

In our earlier example we took c=p/4 and d=p. Thus the integral we need to evaluate is
p

Sin@tD t =

p4

3p
8

Sin
-1

3p
8

x+

5p
8

Let us test out this formula on our previously defined function, which is now represented as

Example
Let us evaluate
3p
8

Sin

3p

-1

x+

5p
8

We define the following function


In[102]:=

g@x_D :=

3p
8

SinB

3p
8

x+

5p
8

Then we use our Newton-Cotes formula


In[103]:=

NewtonCotes2@f_D := ModuleB8<,
1
45

In[104]:=
Out[104]=

In[106]:=
Out[106]=

H7 f@- 1D + 32 f@- 1 2D + 12 f@0D + 32 f@1 2D + 7 f@1DLF

NewtonCotes2@gD N
1.70692
Integrate@g@xD, 8x, - 1, 1<D N
1.70711

ECM6Lecture14bVietnam_2014.nb

Compound Quadrature Rules


In the previous examples our quadrature rule will involve a high order polynomial as the number of data
points increase. This brings on problems of oscillation as the order of the polynomial increases. Thus a
better approach is to use piecewise polynomials.
Thus let N be an integer greater than 1 and let {a,b} be the interval of integration. Then we divide the
interval into N subinterval

a = z0 < z1 < z2 < zN-1 < zN = b


and for each interval we apply the quadrature rule
zk+1

f HxL x

zk

on each subinterval @zk , zk+1 <, and then sum the estimates.

Example 1
Suppose for each subinterval we use the endpoints zk , zk+1 as the interpolation points. Thus we write

f HxL =

Hx - zk+1 L
Hzk - zk+1 L

f Hzk L +

Hx - zk L
Hzk+1 - zk L

f Hzk+1 L

Integrating this expression over the subinterval gives


zk+1

f HxL x =

zk

h
2

8f Hzk L + f Hzk+1 L<

Adding up all the integrals over the subintervals gives


N

zk+1

zk

k=0

f HxL x =
k=0

=h:

1
2

8f Hzk L + f Hzk+1 L<

f Hz0 L + f Hz1 L + f Hz2 L + + f HzN-1 L +

1
2

f HzN L>

This is called the Trapezoidal Quadrature rule. Let us define a Mathematica function to implement the
Trapezoidal rule
In[107]:=

trapzoidalRule@f_, a_, b_, nmax_D := ModuleB8h = Hb - aL nmax<,


NBh

In[108]:=

f@aD
2

nmax-1

f@a + i hD +

i=1

g1@x_D := 10 SinB1 -

x
10

f@bD
2

FF

Here is the value of the integral taking N=30

ECM6Lecture14bVietnam_2014.nb

In[109]:=
Out[109]=

trapzoidalRule@g1, 0, 10, 30D


45.9655

Let us calculate the integral using mathematica's NIntegrate function


In[110]:=
Out[110]=

NIntegrate@g1@xD, 8x, 0, 10<D


45.9698

Example 2
In this example we approximate the function f(x) over a subinterval using a quadratic polynomial. That is
we use 3 interpolation points: zk , zk+12 , zk+1
f HxL =

Hx - zk+12 L Hx - zk+1 L
Hzk - zk+12 L Hzk - zk+1 L
+

f Hzk L +

Hx - zk L Hx - zk+12 L
Hzk+1 - zk L Hzk+1 - zk+12 L

Hx - zk L Hx - zk+1 L
Hzk+12 - zk L Hzk+12 - zk+1 L

f Hzk+12 L

f Hzk+1 L

Integrating this expression over the subinterval gives


zk+1

f HxL x =

zk

:f Hzk L + 4 f K

zk + zk+1
2

O + f Hzk+1 L>

Here h = Hzk+1 - zk L 2. Here is the Mathematica code to do the above calculation.


SimplifyBIntegrateB

Hx - zk+12 L Hx - zk+1 L
Hzk - zk+12 L Hzk - zk+1 L

Hx - zk L Hx - zk+12 L

Hzk+1 - zk L Hzk+1 - zk+12 L


Hzk - z1+k L - 2 h
1
3

h f@zk D + f@z1+k D + 4 fB

1
2

f@zk D +

Hx - zk L Hx - zk+1 L
Hzk+12 - zk L Hzk+12 - zk+1 L

f@zk+12 D

f@zk+1 D, 8x, zk , zk+1 <F . zk+12 Hzk + zk+1 L 2 F .

Hzk + z1+k LF

Adding up all the integrals over the subintervals gives


N

zk+1


k=0

zk

f HxL x =
k=0

h
3

:f Hzk L + 4 f K

zk + zk+1
2

O + f Hzk+1 L>

= h :f Hz0 L + 2 f Hz1 L + 2 f Hz2 L + + 2 f HzN-1 L +


+4 f K

z1 + z2
2

O+4 f K

z2 + z3
2

O++4 f K

zN-1 + zN
2

O + f HzN L>

Now if we take N to be odd so that M=2N then we have M+1 interpolation points (where M is even), this
quadrature rule is called a M-point Simpson's Rule. Let the new interpolation points be x0 , x1 , x2 , , xM ,
where h = Hb - aL M
N

zk+1


k=0

zk

f HxL x =
k=0

h
3

8f Hxk L + 4 f Hxk+1 L + f Hxk+2 L<

ECM6Lecture14bVietnam_2014.nb

= h 8f Hx0 L + 2 f Hx2 L + 2 f Hx4 L + + 2 f HxM-2 L +


+4 f Hx1 L + 4 f Hx3 L + + 4 f HxM-1 L + f HxN L<
This formula can be readily coded in Mathematica as follows
SimpsonRule@f_, a_, b_, nmax_D := ModuleB8M = 2 nmax, h<,
h = Hb - aL M;
h
NB Hf@aD + 4 HSum@ f@a + i hD, 8i, 1, M - 1, 2<DL +
3
2 H Sum@ f@a + i hD, 8i, 2, M - 2, 2<DL + f@bDLFF

Let us evaluate the previous integral using Simpson's rule with N=15
SimpsonRule@g1, 0, 10, 15D
45.9698

ECM6Lecture14bVietnam_2014.nb

References
The following textbooks was used to prepare these notes:
B. Bradie, A friendly Introduction to Numerical Analysis, Pearson/Prentice Hall, 2006
S. Yakowitz and F. Szidarovszky, An Introduction to Numerical Computations, MacMillan Publishing
House, 1986

You might also like