% LU factorization of an n by n matrix A
% using Gauss elimination without pivoting
% LU_Gauss.m
% A is factored as A = L*U
% Output:
% L is lower triangular with the main diagonal part = 1s.
% U is upper triangular and is stored in the upper triangular
% part of A; the remaining part has no special meaning
% and must be zeroed out to get U
% K. Ming Leung, 01/25/03

clear all;
A=[1 2 2
   4 4 2
   4 6 4];
[n,n]=size(A); L=eye(n);
for k=1:n-1
    if (A(k,k) == 0) Error('Pivoting is needed!'); end
    L(k+1:n,k)=A(k+1:n,k)/A(k,k);
    for j=k+1:n
        A(k+1:n,j)=A(k+1:n,j)-A(k,j)*L(k+1:n,k);
    end
end
display(L);
display(A);

