function [x,A] = gauss(A,b)
%
% [x,B] = Gauss(A,b)
% Solve Ax=b by naiv Gauss Elimination
% Input: A and b
% Output: The solution x, 
%         B: The result of the LU-factorization. 
%         U = triu(B); L = tril(B,-1)+eye(n);

n = length(b);
x = zeros(n,1);

% Forward elimination
for k=1:n-1
    for i=k+1:n
        m = A(i,k)/A(k,k);
        for j=k+1:n
            A(i,j) = A(i,j)-m*A(k,j);
        end
        A(i,k)=m;
        b(i) = b(i)-m*b(k);
    end
end

% backward solution
x(n) = b(n)/A(n,n);
for k=n-1:-1:1
    res = b(k);
    for s=k+1:n
        res = res-A(k,s)*x(s);
    end
    x(k) = res/A(k,k); 
end


