function [a,n,tol,o,s,er]= decompose(a,n,tol,er);
% [A]=[L][U]
% break up a(i,j) in [L] and [U] and store it compactly
% in [A]
% function calls ppivot


%begin by initializing the order array
for i = 1: n
    o(i)=i;              %generate order array
    s(i)=abs(a(i,1));    %1st element in the ith row
    
    for j =2 : n           %Loop to check for largest element in row & store in s
        if abs(a(i,j))>s(i)
            s(i)=abs(a(i,j));    
        end
    end
end

%looks like a forward elimination loop with pivoting
for k = 1 :n-1
    [a,o,s,n,k] = ppivot(a,o,s,n,k);
%check for ill-conditioned system by checking near
%zero values of scaled a(i,j) along the diagonal

    if abs(a(o(k),k)/s(o(k))) < tol
        er = -1;
        break
    end
    for i = k+1:n
        factor = a(o(i),k)/a(o(k),k);
        a(o(i),k) = factor;     %store information in lower elements
        for j= k+1:n
            a(o(i),j) = a(o(i),j) - factor*a(o(k),j);
        end
    end
%check for ill-conditioned system
    if abs(a(o(k),k)/s(o(k))) < tol        
        er = -1;
        illcond=a(o(k),k)/s(o(k))
        break
    end
end
