Page 245 - Applied Numerical Methods Using MATLAB
P. 245
234 NUMERICAL DIFFERENTIATION/ INTEGRATION
%nm5e02
%use quad()/quad8() and int() to get CtFS coefficient X16 in Ex 5.2
ftn = ’exp(-j*k*w0*t)’; fcos = inline(ftn,’t’,’k’,’w0’);
P = 4; k = 16; w0 = 2*pi/P;
a=-1;b=1;tol = 0.001; trace = 0;
X16_quad = quad(fcos,a,b,tol,trace,k,w0)
X16_quadl = quadl(fcos,a,b,tol,trace,k,w0)
syms t; % declare symbolic variable
Iexp = int(exp(-j*k*w0*t),t) % symbolic indefinite integral
Icos = int(cos(k*w0*t),t) % symbolic indefinite integral
X16_sym = int(cos(k*w0*t),t,-1,1) % symbolic definite integral
As a numerical approach, we can use the MATLAB routine “quad()”/
“quadl()”. On the other hand, we can also use the MATLAB routine “int()”,
which is a symbolic approach. We put all the statements together to make the
MATLAB program “nm5e02”, in which the fifth input argument (trace)of
“quad()”/“quadl()” is set to 1 so that we can see their nodes and tell how
different they are. Let’s run it and see the results.
>>nm5e02
X16_quad = 0.8150 + 0.0000i %betrayal of MATLAB?
X16_quadl = 7.4771e-008 %almost zero, OK!
Iexp = 1/8*i/pi*exp(-8*i*pi*t) %(E5.2.3) by symbolic computation
Icos = 1/8/pi*sin(8*pi*t) %(E5.2.3) by symbolic computation
X16_sym = 0 %exact answer by symbolic computation
What a surprise! It is totally unexpected that the MATLAB routine “quad()”
gives us a quite eccentric value (0.8150), even without any warning message. The
routine “quad()” must be branded as a betrayer for a piecewise-linear function
multiplied by a periodic function. This seems to imply that “quadl()” is better
than “quad()”and that “int()” is the best of the three commands. It should,
however, be noted that “int()” can directly accept and handle only the functions
composed of basic mathematical functions, rejecting the functions defined in the
form of string or by the “inline()” command or through an m-file and besides,
it takes a long time to execute.
(cf) What about our lovely routine “adapt_smpsn()”? Regrettably, you had better not
count on it, since it will give the wrong answer for this problem. Actually, “quadl()”
is much more reliable than “quad()”and “adapt_smpsn()”.
5.9 GAUSS QUADRATURE
In this section, we cover several kinds of Gauss quadrature methods—that is,
Gauss–Legendre integration, Gauss–Hermite integration, Gauss–Laguerre inte-
gration and Gauss–Chebyshev I,II integration. Each tries to approximate one of