Page 78 - Applied Numerical Methods Using MATLAB
P. 78
PROBLEMS 67
of the difference between the two results? How do you compare
the running times of the two methods?
(cf) Note that Jkb(K,beta) computes J k (β) of order k = 1:K, while the inte-
gration does for only k = K.
function [J,JJ] = Jkb(K,beta) %the 1st kind of kth-order Bessel ftn
tmpk = ones(size(beta));
for k = 0:K
tmp = tmpk; JJ(k + 1,:) = tmp;
for m = 1:100
tmp = ?????????????????????;
JJ(k + 1,:) = JJ(k + 1,:)+ tmp;
if norm(tmp)<.001, break; end
end
tmpk = tmpk.*beta/2/(k + 1);
end
J = JJ(K+1,:);
%nm1p21b: Bessel_ftn
clear, clf
beta = 0:.05:15; K = 15;
tic
for i = 1:length(beta) %Integration
J151(i) = quad(’Bessel_integrand’,0,pi,[],0,beta(i),K)/pi;
end
toc
tic, J152 = Jkb(K,beta); toc %Recursive Computation
discrepancy = norm(J151-J152)
1.22 Find the four routines in Chapter 5 and 7, which are fabricated in a nested
(recursive calling) structure.
(cf) Don’t those algorithms, which are the souls of the routines, seem to have been
born to be in a nested structure?
1.23 Avoiding Runtime Error in Case of Deficient/Nonadmissible Input Argu-
ments
(a) Consider the MATLAB routine “rotation_r(x,M)”, which you made
in Problem 1.15(h). Does it work somehow when the user gives a
negative integer as the second input argument M ? If not, add a statement
so that it performs the rotation left by −M samples for M < 0, say,
making
rotate_r([1234 5],-2) = [34512]
(b) Consider the routine ‘trpzds(f,a,b,N)’ in Section 5.6, which com-
putes 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.