% x = gauss_elim(A,b) takes a square matrix A and a vector b and 
% returns the solution of Ax=b.

function x = gauss_elim(A,b)

[n,n] = size(A);

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

% now the matrix A is in an upper triangular form.  Ignore the numbers
% below the diagonal, they should be zero, I just didn't force them to
% be.

x(n) = b(n)/A(n,n);
for i=1:n-1
  x(n-i) = b(n-i);
  for j=n-i+1:n
    x(n-i) = x(n-i) - A(n-i,j)*x(j);
  end
  x(n-i) = x(n-i)/A(n-i,n-i);
end

x = x';