Professional Documents
Culture Documents
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
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 =
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]:=
Out[101]=
45
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
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
1
2
1
2
=1.70711
ECM6Lecture14bVietnam_2014.nb
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
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
d-c
2
, Q=
c+d
2
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
g@x_D :=
3p
8
SinB
3p
8
x+
5p
8
NewtonCotes2@f_D := ModuleB8<,
1
45
In[104]:=
Out[104]=
In[106]:=
Out[106]=
NewtonCotes2@gD N
1.70692
Integrate@g@xD, 8x, - 1, 1<D N
1.70711
ECM6Lecture14bVietnam_2014.nb
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
f HxL x =
zk
h
2
zk+1
zk
k=0
f HxL x =
k=0
=h:
1
2
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]:=
In[108]:=
f@aD
2
nmax-1
f@a + i hD +
i=1
g1@x_D := 10 SinB1 -
x
10
f@bD
2
FF
ECM6Lecture14bVietnam_2014.nb
In[109]:=
Out[109]=
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
f HxL x =
zk
:f Hzk L + 4 f K
zk + zk+1
2
O + f Hzk+1 L>
Hx - zk+12 L Hx - zk+1 L
Hzk - zk+12 L Hzk - zk+1 L
Hx - zk L Hx - zk+12 L
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
Hzk + z1+k LF
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>
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
ECM6Lecture14bVietnam_2014.nb
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