Cholesky Decomposition: Switch Case
Cholesky Decomposition: Switch Case
disp('***************************MAIN
MENU*************************');
disp('Applications of Cholesky Decomposition');
disp('1. Solve a system of linear equations');
disp('2. Find inverse of a matrix');
choice=input('Enter your choice:');
switch choice
case 1
A=input('Enter matrix A:');
L=cholesky2(A)
b=input('Enter matrix B:');
z=L\b;
Forward substitution
disp('Solution Vector')
x=L'\z
Backward substitution
case 2
A=input('Enter matrix A:');
L=cholesky2(A)
disp('Inverse of A :')
Ainv= inv(L')*inv(L)
otherwise
disp('Wrong Choice!!')
return;
end
%*************END of the Program *******************
%
%
function [ L ] = cholesky2( A )
[m,n]=size(A);
if m~=n
disp('Matrix shoud be a square matrix');
elseif A~=A'
fprintf('Given matrix is not symmetric. \n Cholesky Method
doesnot apply to asymmetric matrices!\n');
elseif length(find(eig(A)>0))~=length(eig(A))
fprintf('Given matrix is not positive definite. \n Cholesky
Method doesnot apply to non-positive definite matrices!\n');
else
L = zeros( n, n );
for j=1:n
for i=j:n
sum=0;
if i==j
for k=1 : j-1
sum = sum + L(j,k)^2;
end
L(j,j)=sqrt(A(j,j)-sum);
else
end
end
end
output:
METHOD 2function A=Cholesky(A)
% Cholesky Factorization for symmetric positive definite matrix