You are on page 1of 5

Quadrature formulas - Gaussian quadrature

Math 5172 - Finite Element Method


Section 001, Spring 2010
When implementing FEM for the following one-dimensional Sturm-Liouville problem
0

(pu0) + qu = f,
we need to evaluate the following integrals
Z xR
p0i0j dx,

xL

a < x < b,

xR

qi j dx,
xL

xR

fj dx,
xL

where [xL, xR] represents an element. In general, these integrals shall NOT be pre-evaluated manually and then
programmed in the code. Instead they should be evaluated by a numerical quadrature.

(1) Simpsons rule and its variants:


The standard Simpsons rule is
Z





ba
a+b
g(a) + 4g
+ g(b) .
6
2

g(x)dx S[g] =
a

The error of the standard Simpsons rule is




Z b

(b a)5


g(x)dx =
|f (4) ()|,
S[g]


2880
a

(a, b).

Note that the Simpsons rule provides the exact integral if g(x) is a polynomial of degree 3 or less. Also because
of that prefactor of 1/2880, this method is quite accurate because b a h is small in FEM.
Adaptive Simpsons rule: subdivide [a, b] into multiple subintervals, and then apply the Simpsons rule to
each subinterval. The Matlab built-in quadrature quad() uses Adaptive Simpsons rule.
The Simpsons 3/8 rule: is
Z

=
g(x)dx S[g]
a







ba
2a + b
a + 2b
g(a) + 3g
+ 3g
+ g(b) .
8
3
3

The error of the Simpsons 3/8 rule is




Z b

(b a)5


g(x)dx =
|f (4) ()|,
S[g]


6480
a

(2) Gaussian quadrature for

g()d:
1

(a) Basic idea: The general quadrature formula can be written as


Z

g()d =
1

N
X
i=1

wig(i ),

(a, b).

where i and wi are called quadrature points and weights. We want to choose i and wi so that the
quadrature is as accurate as possible. In the mid-point rule, the trapezoidal rule, and the Simpsons
methods, i are pre-defined (e.g. as end- and mid-points of the interval). In the Gaussian quadratures,
however, both i and wi are unknowns. Generally we can choose i and wi such that the quadrature
formula is exact for g() = 1, , , 2N 1. The number 2N 1 is called the algebraic precision of the
quadrature formula. Such quadrature formulas have the following characteristics:
Very accurate: A Gaussian quadrature using N points can provide the exact integral if g() is a
polynomial of degree 2N 1 or less.
Open formulas: do not need to use two end points. (Otherwise, use Gauss-Radau or Gauss-Lobatto
quadrature)
No recursive relations for i and wi.
Good for finite element methods because b a h is small; we just need a few points i .
We call i and wi Gaussian points and weights of the quadrature formulas.
(b) Gaussian quadrature of order 1 (one point):
Z

g()d = w1g(1 ).
1

It should be exact for g() = 1 and g() = . We get


Z 1
g() = 1,
g()d = 2 2 = w1 1,
g() = ,

1
Z 1

g()d = 0

0 = w1 1 ,

w1 = 2,
1 = 0.

Therefore, 1 = 0 and w1 = 2. The quadrature formula is the mid-point rule:


Z 1
g()d 2g(0).
1

(c) Gaussian quadrature of order 2 (two points):


Z

g()d = w1g(1 ) + w2 g(2 ).


1

It should be exact for g() = 1, g() = , g() = 2 and g() = 3 . We get


Z 1
g() = 1,
g()d = 2 2 = w1 + w2
g() = ,
g() = 2 ,
g() = 3 ,

1
1

g()d = 0

1
Z 1

g()d =

1
1

2
3

g()d = 0

0 = w11 + w2 2 ,

2
= w112 + w2 22,
3

0 = w113 + w2 23.

Solving the non-linear system of equations (how?), we get


1
1 = ,
3

w1 = w2 = 1,
2

1
2 = .
3

Therefore, the Gaussian quadrature of order 2 is






Z 1
1
1
g()d g
+g
.
3
3
1
(d) Gaussian quadrature of order 3 (three points):
Similarly, we can get the Gaussian quadrature of order 3:
!
Z 1
3
5
5
8
g()d g
+ g (0) + g
9
9
9
5
1

!
3

.
5

Gaussian points and weights for 4 N 8 are given in Appendix.

(3) How to use Gaussian quadrature for

f(x)dx:
a

f(x)dx =
a

f()d
1

xa xb
ba
+
, or x = a +
(1 + )
ba
ba
2
2
ba
d =
dx, dx =
d.
ba
2


Z b
Z
ba 1
ba
f(x)dx =
f a+
(1 + ) d.
2
2
a
1
=

Therefore, we have

b
a

ba X
f(x)dx
wi f
2
N

i=1



ba
a+
(1 + i ) .
2

(4) The Matlab code using Gaussian quadrature to evaluate


Z b
I=
f(x)dx
a

is int f.m. It uses f.m which defines the function f(x) and GaussPoints.m which provides Gaussian points
and weights. The calling sequence of int f.m is
int_f(quadrature_order, lower_limit_a, upper_limit_b)
A simple test: Consider the following integral
Z 1

I=
x9 + x4 + 2x + 1 = 2.3
0

Since f(x) = x9 + x4 + 2x + 1 has degree of 9, Gaussian quadrature with N = 5 should be able to provide the
exact integral. Actually we have
(i) int f(4, 0, 1) returns 2.299897959183662, which is close to the exact one but definitely not exact!
(ii) int f(5, 0, 1) returns the exact value 2.3.
(iii) int f(6, 0, 1) returns the exact value 2.3.

Appendix - Matlab codes


(1) Main routine: int f.m

function y = int_f(N,a,b)
%
%

This function evaluates \int_a^b f(x) dx using


the Gaussian quadrature of order N.
xw = GaussPoints(N); % get quadrature points and weights
y = 0.0;
for j = 1:N
x = a + 0.5*(b-a)*(1+xw(j,1));
y = y + f(x)*xw(j,2);
end
y = 0.5*(b-a)*y;
return

(2) Function to define f(x): f.m

function y = f(x)
y = x^9+x^4+2*x+1;
return

(3) Function to provide Gaussian points i and weights wi: GaussPoints.m

function xw = GaussPoints(n)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
Function GaussPoints provides the Gaussian points x(i) and weights %
%
w(i) for the Gaussian quadrature of order n.
%
%
%
%
Input: n
- the order of the Gaussian quadrature
%
%
%
%
Output: xw - a n by 2 matrix: First column gives the points
%
%
Second column gives the weights
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xw = zeros(n,2);
if (n == 1)
xw=[ 0.00000000000000

2.00000000000000];

elseif (n == 2)
xw=[-0.57735026918963
0.57735026918963

1.00000000000000
1.00000000000000];

elseif (n == 3)
xw=[-0.77459666924148
0.00000000000000
0.77459666924148

0.55555555555555
0.88888888888889
0.55555555555555];

elseif (n == 4)
xw=[-0.86113631159405
-0.33998104358486
0.33998104358486
0.86113631159405

0.34785484513745
0.65214515486254
0.65214515486255
0.34785484513745];

elseif (n == 5)
xw=[-0.90617984593866
-0.53846931010568
0.00000000000000
0.53846931010568
0.90617984593866

0.23692688505619
0.47862867049937
0.56888888888889
0.47862867049937
0.23692688505619];

elseif (n == 6)
xw=[-0.93246951420315
-0.66120938646626
-0.23861918608320
0.23861918608320
0.66120938646626
0.93246951420315

0.17132449237917
0.36076157304814
0.46791393457269
0.46791393457269
0.36076157304814
0.17132449237917];

elseif (n == 7)
xw=[-0.94910791234276
-0.74153118559939
-0.40584515137740
0.00000000000000
0.40584515137740
0.74153118559939
0.94910791234276

0.12948496616887
0.27970539148928
0.38183005050512
0.41795918367347
0.38183005050512
0.27970539148928
0.12948496616887];

elseif (n == 8)
xw=[-0.96028985649754
-0.79666647741363
-0.52553240991633
-0.18343464249565
0.18343464249565
0.52553240991633
0.79666647741363
0.96028985649754

0.10122853629038
0.22238103445337
0.31370664587789
0.36268378337836
0.36268378337836
0.31370664587789
0.22238103445337
0.10122853629038];

else
error(Bad input n);
end
return

You might also like