function [L,U, f] = lu_no_pivot(A)
%LU  Triangular factorization, textbook version, no pivot
%   [L,U, f] = lu_no_pivot(A) produces a unit lower triangular matrix L,
%   an upper triangular matrix U
%   so that L*U = A
%   f=1 if there is LU factorization, f=0 if not.

[n,n] = size(A);

f=1;

for k = 1:n-1
   % Skip elimination if column is zero
   if (A(k,k) == 0) 
       f=0;
       error('zero pivot')
   end
      % Compute multipliers
      i = k+1:n;
      A(i,k) = A(i,k)/A(k,k);

      % Update the remainder of the matrix
      j = k+1:n;
      A(i,j) = A(i,j) - A(i,k)*A(k,j); 
end

% Separate result
L = tril(A,-1) + eye(n,n);
U = triu(A);
