Computes the Cholesky factorization of A Input arguments: A, symmetric positive definite matrix Output arguments: Q, square matrix of the same dimensions as A such that A=Q*Q'
0001 function [ Q ] = Cholesky( A ) 0002 % Computes the Cholesky factorization of A 0003 % Input arguments: 0004 % A, symmetric positive definite matrix 0005 % Output arguments: 0006 % Q, square matrix of the same dimensions as A such that A=Q*Q' 0007 0008 [n,m]=size(A); % finding the size of A 0009 if n~=m; 0010 error('input is not a square matrix'); 0011 end 0012 0013 L=eye(n); D=eye(n); Q=eye(n); % initializing L,D,Q to the identity matrix 0014 U=A; % initializing U to be A 0015 for i=1:n; % first generate the LDL' factorization of A 0016 D(i,i)=U(i,i); 0017 if D(i,i)==0; % if any element of D is zero then A is singular 0018 error('Matrix is singular'); 0019 end 0020 L(:,i)=U(:,i)/U(i,i); 0021 if D(i,i)<0; % if any element of D is negative then A is not 0022 % positive definite 0023 error('Matrix is not positive definite'); 0024 end 0025 U=U-D(i,i)*L(:,i)*L(:,i)'; 0026 end 0027 0028 D=D.^(1/2); % '.^' performs element-wise exponentiation 0029 Q=L*D; 0030 0031 end