Numerical Integration

Gauss‐Legendre quadrature is used to calculate integrals 1
 f ( x)dx  c
0
f ( x0 )  c1 f ( x1 )    cn 1 f ( xn 1 )
1
The locations and weights for different values of n are given in Table 18.1 b
Integrals of the form  f ( x)dx are transformed to the right limits by a change of variable a
x
(b  a) (b  a )

xd 2
2
ba
ba
And then  f ( x)dx 
f d ( xd )dxd 
c0 f d ( x0 )  c1 f d ( x1 )    cn1 f d ( xn1 ) 
2 1
2
a
b
1
 b  a 
Where f d ( xi )  f 
 xi 
 2 
ba
2 
Problem 18.4 The error function is erf (a) 
2
a
e
 
 x2
dx 0
Calculate erf(1.5) it by 2‐point and 3‐point Gauss‐Legendre quadrature. Compare to the exact value given by Matlab function erf >> derf=@(x)2/sqrt(pi)*exp(‐x.^2) derf = @(x)2/sqrt(pi)*exp(‐x^2) >> a=0;b=1.5;x(1)=‐1/sqrt(3);x(2)=‐x(1) x = ‐0.5774 0.5774 >> intgl2=(derf(xm(1))+derf(xm(2)))*(b‐a)/2 intgl2 = 0.9742 Compared to the exact integral >> erf(1.5) ans = 0.9661 An error of about 0.8% With three points >> x(1)=‐sqrt(0.6);x(2)=0;x(3)=‐x(1) x = ‐0.7746 0 0.7746 >> xm=(b+a)/2+(b‐a)*x/2 xm = 0.1691 0.7500 1.3309 >> c(1)=5/9;c(2)=8/9;c(3)=5/9 c = 0.5556 0.8889 0.5556 >> intgl2=derf(xm)*c'*(b‐a)/2 intgl2 = 0.9655 Now the error is 0.06%. For comparison take trapezoidal integration with 2 and 3 points. >> xt=[0 1.5]; yt=derf(xt) yt = 1.1284 0.1189 >> trap2=trapz(xt,yt) trap2 = 0.9355 >> xt=linspace(0,1.5,3) xt = 0 0.7500 1.5000 >> yt=derf(xt) yt = 1.1284 0.6429 0.1189 >> trap3=trapz(xt,yt) trap3 = 0.9499 The error reduces much more slowly. We can also apply Richardson extrapolation to improve the result >> richardson=(4/3)*trap3‐trap2/3 richardson = 0.9548 We can also try Matlab adaptive integration routine quad (good for low accuracy, non‐smooth functions) or quadl (high accuracy smooth functions) >> [intq,fcnt]=quad(derf,0,1.5,0.001) intq = 0.9661 fcnt = 13 >> [intq,fcnt]=quadl(derf,0,1.5,0.001) intq = 0.9661 fcnt = 18 Good accuracy but many evalutions.