Applied Numerical Methods Using MATLAB. Won Y. Yang
that are fabricated in a recursive (self‐calling) structure.(cf) Does not those algorithms, which are the souls of the functions, seem to have been born to be in a nested structure?
25 1.25 Avoiding Runtime Error in Case of Deficient/Non‐admissible Input ArgumentsConsider the MATLAB function ‘ rotate_r(x,M)’, that you made in Problem 1.17(h). Does it work somehow when the user gives a negative integer as the second input argument M? If not, modify it so that it can perform the rotation left by – M samples for M<0, say, making rotate_r([1 2 3 4 5],-2)=[3 4 5 1 2]Consider the function ‘ trpzds(f,a,b,N)’ in Section 5.6, which computes the integral of function f over [ a, b] by dividing the integration interval into N sections and applying the trapezoidal rule. If the user tries to use it without the fourth input argument N, will it work? If not, make it work with N = 1000 by default even without the fourth input argument N.function INTf=trpzds(f,a,b,N) %integral of f(x) over [a,b] by trapezoidal rule with N segments if abs(b-a)<eps|N<=0, INTf=0; return; end h=(b-a)/N; x=a+[0:N]*h; fx=feval(f,x); values of f for all nodes INTf= h*((fx(1)+fx(N+1))/2+sum(fx(2:N))); % Eq.(5.6.1)
26 1.26 Parameter Passing Through vararginConsider the integration function ‘ trpzds(f,a,b,N)’ in Section 5.6. Can you use it to compute the integral of a function with some parameter(s), like the ‘ Bessel_integrand(x,beta,k)’ that you defined in Problem 1.23? If not, modify it so that it works for a function with some parameter(s) (see Section 1.3.6) and save it in the M‐file named “trpzds_par.m”. Then replace the ‘ quad()’ statement in the script “nm01p23b.m” (presented in Problem 1.23) by an appropriate ‘ trpzds_par()’ statement (with N= 1000) and run the script. What is the discrepancy between the integration results obtained by this function and the nested computing based on Eq. (P1.23.4b)? Is it comparable with that obtained with ‘ quad()’? How do you compare the running time of this function with that of ‘ quad()’? Why do you think it takes so much time to execute the ‘ quad()’ function?
27 1.27 Adaptive Input Argument to Avoid Runtime Error in Case of Different Input ArgumentsConsider the integration function ‘ trpzds(f,a,b,N)’ in Section 5.6. If some user tries to use this function with the following statement, will it work? trpzds(f,[a b],N) or trpzds(f,[a b])function INTf=<b>trpzds_bnd</b><![CDATA[(f,a,b,N) if numel(a)==2 if nargin>2, N=b; else N=1000; end b=a(?); a=a(?); else if nargin<4, N=1000; end end % . . . . . . . . . . . . . . . . . . . . .If not, modify it so that it works for such a usage (with a bound vector as the second input argument) as well as for the standard usage and save it in the M‐file named “trpzds_bnd.m”. Then try it to find the integral of e−t for [0,100] by running the following statements. What did you get?
28 1.28Continuous‐time Fourier transform (CtFT) of a SignalConsider the following definitions of CtFT and inverse continuous‐time Fourier transform (ICtFT) [W-8].(P1.28.1a) (P1.28.1b) Complete the following two MATLAB functions, ‘ CtFT1(x,Dt,w)’ computing the CtFT (P1.28.1a) of x(t) over [ ‐Dt,Dt] for w and ‘ ICtFT1(X,Bw,t)’ computing the ICtFT (P1.28.1b) of X(w) over [‐Bw, Bw] for t. You can choose whatever integral function including ‘ trpzds_par()’ (Problem 1.25) and ‘ quad()’, considering the running time.The following script “nm01p28.m” finds the CtFT of a rectangular pulse (with duration [ −1,1]) defined by ‘ rDt()’ for ω = [ −6π,+6π] and the ICtFT of a sinc spectrum (with bandwidth 2π) defined by ‘ sincBw()’ for t = [ −5,+5]. After having saved the functions and script into M‐files with the appropriate names, run the script to see the rectangular pulse, its CtFT spectrum, a sinc spectrum, and its ICtFT as shown in Figure P1.28. If it does not work, modify/supplement the functions so that you can rerun it to see the signals and their CtFT spectra.Figure P1.28 Graphs for Problem 1.28. (a1) A rectangular pulse function rD(t); (a2) the ICtFT of XB(ω); (b1) the CtFT spectrum of rD(t); and (b2) a sinc spectrum XB(ω).function Xw=<b>CtFT1</b><![CDATA[(x,Dt,w) % CtFT (Continuous-time Fourier Transform) x_ejkwt=inline([x '(t).*exp(-j*w*t)'],'t','w'); Xw=trpzds_par(x_ejkwt,-Dt,??,1000,?);function xt=<b>ICtFT1</b><![CDATA[(X,Bw,t) % ICtFT (Inverse Continuous-time Fourier Transform) Xejkwt=inline([X '(w).*exp(j*w*t)'],'w','t'); xt=trpzds_par(Xejkwt,-??,Bw,1000,?)/2/pi;%nm01p28.m : CtFT and ICtFT global B D % CtFT of A Rectangular Pulse Function t=[-50:50]/10; % Time vector w=[-60:60]/10*pi; % Frequency vector D=1; % Duration of a rectangular pulse rD(t) for k=1:length(w) Xw(k)=CtFT1('rDt',D*5,w(k)); end subplot(221), plot(t,rDt(t)); subplot(222), plot(w,abs(Xw)) % ICtFT of a Sinc Spectrum B=2*pi; % Bandwidth of a sinc spectrum sncB(w) for n=1:length(t) xt(n)=ICtFT1('sincBw',B*5,t(n)); end subplot(223), plot(t,real(xt)); subplot(224), plot(w,sincBw(w))function x=<b>rDt</b><![CDATA[(t) % Rectangular pulse function global D x=(-D/2<=t?t<=D/2);function X=<b>sincBw</b><![CDATA[(w) % Sinc function global B X=2*pi/?*sinc(?/B);
Конец ознакомительного фрагмента.
Текст предоставлен ООО «ЛитРес».
Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.
Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.