Page 69 - Applied Numerical Methods Using MATLAB
P. 69
58 MATLAB USAGE AND COMPUTATIONAL ERRORS
the matrix multiplication can be described as
K
C(m, n) = A(m, k)B(k, n) (P1.12.1)
k=1
function C = multiply_matrix(A,B)
[M,K] = size(A); [K1,N] = size(B);
if K1 ~= K
error(’The # of columns of A is not equal to the # of rows of B’)
else
form=1:
forn=1:
C(m,n) = A(m,1)*B(1,n);
fork=2:
C(m,n) = C(m,n) + A(m,k)*B(k,n);
end
end
end
end
1.13 Function for Finding Vector Norm
Assuming that MATLAB does not have the norm() command finding us the
norm of a given vector/matrix, make a routine norm_vector(v,p),which
computes the norm of a given vector as
N
p
||v|| p = p |v n | (P1.13.1)
n=1
for any positive integer p, finds the maximum absolute value of the elements
for p = inf and computes the norm as if p=2, even if the second input
argument p is not given. If you have no idea, permutate the statements in the
below box and save it in the file named “norm_vector.m”. Additionally, try
it to get the norm with p= 1,2,∞ (inf) and of an arbitrary vector generated
by the command rand(2,1). Compare the result with that obtained by using
the norm() command.
function nv = norm_vector(v,p)
if nargin < 2, p = 2; end
nv = sum(abs(v).^p)^(1/p);
nv = max(abs(v));
ifp>0&p~=inf
elseif p == inf
end