Exploration 3.3.12 Write a Matlab function [G,isposdef]=cholesky(A) that impleme
ID: 2261869 • Letter: E
Question
Exploration 3.3.12
Write a Matlab function [G,isposdef]=cholesky(A) that implements Algorithm 3.3.5. The second output argument isposdef is a logical variable that has the value true if A is in fact symmetric positive definite. Your function should ensure that this variable is set to false if the Cholesky Factorization should break down for any reason.
Algorithm 3.3.5 (Cholesky Factorization) Given a A R n×n that is symmetric positive definite matrix, the following algorithm computes the Cholesky Factorization A = GG^T , where G is lower triangular with positive diagonal entries.
for j = 1, 2, . . . , n do
gjj = ajj
for i = j + 1, j + 2, . . . , n do
gij = aij/gjj
for k = j + 1, . . . , i do
aik = aik gijgkj
end for
end for
end for
Explanation / Answer
MATLAB CODE
function [G,isposdef]=cholesky(A)
%Function to check whether a given matrix A is positive definite
%Returns isposdef=1, if the input matrix is positive definite
%Returns isposdef=0, if the input matrix is not positive definite
%Throws error if the input matrix is not symmetric
A=[7 0;0 8];
%Check if the matrix is symmetric
[n,n]=size(A);
%L=zeros(m,n)
if ~isequal(A,A'),
error('Input Matrix is not Symmetric');
end
% if m~=n,
% error('A is not Square');
% end
%Test for positive definiteness
isposdef=1; %Flag to check for positiveness
for i=1:n
subA=A(1:i,1:i); %Extract upper left kxk submatrix
if(det(subA)<=0); %Check if the determinent of the kxk submatrix is +ve
isposdef=0;
break;
end
end
if isposdef
display('Given Matrix is Positive definite');
for k=1:n
A(k,k)=sqrt(A(k,k));
A(k+1:n,k)=A(k+1:n,k)/A(k,k);
for j=k+1:n
A(j:n,j)=A(j:n,j)-A(j,k)*A(j:n,k);
end
end
G=A;
%L*L'
else
display('Given Matrix is NOT positive definite');
end
end
Output
Cholesky
Given Matrix is Positive definite
ans =
2.6458 0
0 2.8284
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.