function U = cholesky(A)
% Function to demonstrate the Cholesky factorization of a symmetric, 
%  positive-definite matrix A such that transpose(U)U = A
%
% A is nxn symmetric, positive definite matrix
% U is nxn upper triangular matrix
% Syntax: U = cholesky(A)

[rA,cA] = size(A);  % Determine the size of A

% Input checks
if rA ~= cA
    disp('Error: A is not a square matrix')
    return
else
    n = rA;
end
if ~issymmetric(A)
    disp('Error: A is not symmetric')
    return
end


% Initialize U matrix
U = zeros(n);

disp(' ')
disp('Original Matrix [A]')
disp('-----------------------------')
disp(A)
disp(' ')

for i = 1:n
    disp(['Row ',num2str(i)])
    disp('-----------------------------')
    sum = 0;
    for k = 1:i-1
        sum = sum + U(k,i)*U(k,i);
    end
    U(i,i) = sqrt(A(i,i)-sum);
    for j = i+1:n
        sum = 0;
        for k = 1:i-1
            sum = sum + U(k,i)*U(k,j);
        end
        U(i,j) = (A(i,j) - sum)/U(i,i);
    end
    disp(U)
    disp(' ')   
end