Page 261 - Applied Numerical Methods Using MATLAB
P. 261
250 NUMERICAL DIFFERENTIATION/ INTEGRATION
(ii) Add the block of statements in P5.5(c) into the routines “smp-
sns()”and “adap_smpsn()” to make them cope with the cases
of NaN (Not-a-Number) and Inf (Infinity).
(iii) Supplement the program “nm5p06a.m” so that the various routines
are applied for computing the integrals (P5.6.1) and (P5.6.3), where
the parameters like the number of segments (N = 200), the error
tolerance (tol = 1e-4), and the number of grid points (MGL = 20)
are supposed to be used as they are in the program. Noting that
the second integrand function in (P5.6.3) oscillates like crazy with
higher frequency and larger amplitude as y gets closer to zero (0),
set the lower bound of the integration interval to a2 = 0.001.
(iv) Run the supplemented program and fill in Table P5.6 with the
absolute errors of the results.
%nm5p06a
warning off MATLAB:divideByZero
fp56a = inline(’sin(x)./x’,’x’); fp56a2 = inline(’sin(1./y)./y’,’y’);
IT = pi/2; % True value of the integral
a = 0; b = 100; N = 200; tol = 1e-4; MGL = 20; a1 = 0; b1 = 1; a2 = 0.001; b2 = 1;
format short e
e_s = smpsns(fp56a,a,b,N)-IT
e_as = adapt_smpsn(fp56a,a,b,tol)-IT
e_ql = quadl(fp56a,a,b,tol)-IT
e_GL = Gauss_Legendre(fp56a,a,b,MGL)-IT
e_ss = smpsns(fp56a,a1,b1,N) + smpsns(fp56a2,a2,b2,N)-IT
e_Iasas = adapt_smpsn(fp56a,a1,b1,tol)+ ...
???????????????????????????? -IT
e_Iqq = quad(fp56a,a1,b1,tol)+??????????????????????????? -IT
warning on MATLAB:divideByZero
%nm5p06b
warning off MATLAB:divideByZero
fp56b = inline(’exp(-x.*x)’,’x’);
fp56b1 = inline(’ones(size(x))’,’x’);
fp56b2 = inline(’exp(-1./y./y)./y./y’,’y’);
a = 0; b = 200; N = 200; tol = 1e-4; IT = sqrt(pi)/2;
a1 =0;b1=1;a2=0;b2=1; MGH=2;
e_s = smpsns(fp56b,a,b,N)-IT
e_as = adapt_smpsn(fp56b,a,b,tol)-IT
e_q = quad(fp56b,a,b,tol)-IT
e_GH = Gauss_Hermite(fp56b1,MGH)/2-IT
e_ss = smpsns(fp56b,a1,b1,N) + smpsns(fp56b2,a2,b2,N)-IT
Iasas = adapt_smpsn(fp56b,a1,b1,tol)+ ...
+????????????????????????????? -IT
e_qq = quad(fp56b,a1,b1,tol)+????????????????????????? -IT
warning off MATLAB:divideByZero