%NAME : Aditya Raj
%ROLL NUMBER: 20UEC007
function [U,S,V] = svd4(A)
[n,m] = size(A);
% Check if matrix is valid
if n ~= m
error('Error: Matrix must be square for SVD decomposition.');
end
U = eye(n);
V = eye(m);
tol = max(abs(A(:)))*1.e-15;
Arem = inf;
while Arem > tol
[Qu,Ru] = qr(A);
U = U*Qu;
[Qv,Rv] = qr(Ru');
V = V*Qv;
A = Rv';
Arem = norm(tril(A,-1),inf);
end
% Check if the matrix is valid after the loop
if Arem > tol
error('Error: Matrix cannot be decomposed.');
end
U = U(:,1:m);
S = triu(A(1:m,:));
% correct any negative singular values
V = V.*sign(diag(S)).';
S = abs(S);
disp('U:');
disp(U);
disp('S:');
disp(S);
disp('V:');
disp(V);
end
% Example usage:
A = [1 2 3; 4 5 6; 7 8 9];
try
[U, S, V] = svd4(A);
1
catch ME
disp(ME.message);
end
U:
-0.2148 0.8872 0.4082
-0.5206 0.2496 -0.8165
-0.8263 -0.3879 0.4082
S:
16.8481 0 0
0 1.0684 0
0 0 0.0000
V:
-0.4797 -0.7767 -0.4082
-0.5724 -0.0757 0.8165
-0.6651 0.6253 -0.4082
Published with MATLAB® R2024a
2
%NAME : Aditya Raj
%ROLL NUMBER: 20UEC007
function [U,S,V] = svd4(A)
[n,m] = size(A);
% Check if matrix is valid
if n ~= m
error('Error: Matrix must be square for SVD decomposition.');
end
U = eye(n);
V = eye(m);
tol = max(abs(A(:)))*1.e-15;
Arem = inf;
while Arem > tol
[Qu,Ru] = qr(A);
U = U*Qu;
[Qv,Rv] = qr(Ru');
V = V*Qv;
A = Rv';
Arem = norm(tril(A,-1),inf);
end
% Check if the matrix is valid after the loop
if Arem > tol
error('Error: Matrix cannot be decomposed.');
end
U = U(:,1:m);
S = triu(A(1:m,:));
% correct any negative singular values
V = V.*sign(diag(S)).';
S = abs(S);
disp('U:');
disp(U);
disp('S:');
disp(S);
disp('V:');
disp(V);
end
% Example usage:
A = [1 2; 4 5; 7 8];
try
[U, S, V] = svd4(A);
1
catch ME
disp(ME.message);
end
Error: Matrix must be square for SVD decomposition.
Published with MATLAB® R2024a
2
%NAME: Aditya Raj
%Roll no: 20uec007
function compute_svd_and_inverse(matrix)
try
% Measure time to compute SVD decomposition
tic;
[U, S, V] = svd(matrix);
disp('U:');
disp(U);
disp('S:');
disp(S);
disp('V:');
disp(V);
svd_time = toc;
% Measure time to compute inverse without decomposition
tic;
inv_matrix = inv(matrix);
inv_time_without_svd = toc;
% Measure time to compute inverse with decomposition
tic;
inv_matrix_svd = pinv(matrix);
inv_time_with_svd = toc;
% Display times
disp(['Time to compute SVD decomposition: ' num2str(svd_time) '
seconds']);
disp(['Time to compute inverse without SVD: '
num2str(inv_time_without_svd) ' seconds']);
disp(['Time to compute inverse with SVD: ' num2str(inv_time_with_svd)
' seconds']);
catch ME
% Error handling
if strcmp(ME.identifier, 'MATLAB:svd:matrixWithNaNInf')
disp('Error: The matrix contains NaN or Inf values.');
elseif strcmp(ME.identifier, 'MATLAB:singularMatrix')
disp('Error: The matrix cannot be decomposed.');
else
disp('An unexpected error occurred.');
end
end
end
% Example usage:
% Define a matrix
A = [1 2 3; 4 5 6; 7 8 9];
% Compute SVD decomposition and inversion
compute_svd_and_inverse(A);
1
U:
-0.2148 0.8872 0.4082
-0.5206 0.2496 -0.8165
-0.8263 -0.3879 0.4082
S:
16.8481 0 0
0 1.0684 0
0 0 0.0000
V:
-0.4797 -0.7767 0.4082
-0.5724 -0.0757 -0.8165
-0.6651 0.6253 0.4082
Warning: Matrix is close to singular or badly scaled. Results may be
inaccurate.
RCOND = 1.541976e-18.
Time to compute SVD decomposition: 0.000434 seconds
Time to compute inverse without SVD: 0.000513 seconds
Time to compute inverse with SVD: 0.006459 seconds
Published with MATLAB® R2024a