## Gauss elimination with pivoting
## Author: M. Turhan COBAN
function x=gauss1(a,b)
% Gauss elimination
n=length(b);      
carpan=0;
toplam=0;
  for k=1:n
    %pivoting
    p=k;
    buyuk=abs(a(k,k));
    for ii=k+1:n
      dummy=abs(a(ii,k));
       if dummy > buyuk buyuk=dummy;p=ii;end
    end
    if p~=k
      for jj=k:n
        dummy=a(p,jj);
        a(p,jj)=a(k,jj);
        a(k,jj)=dummy;
        end
      dummy=b(p);
      b(p)=b(k);
      b(k)=dummy;
    end
    %gauss eleme
    for i=k+1:n
      carpan=a(i,k)/a(k,k);
       a(i,k)=0;
       for j=k+1:n
          a(i,j)=a(i,j)-carpan*a(k,j); 
       end
           b(i)   =b(i)   -carpan*b(k);
    end
  end
  %back substituting
  x(n)=b(n)/a(n,n);
  for i=n-1:-1:1
    toplam=0;
    for j=i+1:n
      toplam=toplam+a(i,j)*x(j);end
    x(i)=(b(i)-toplam)/a(i,i);
  end
end

