function u = Gauss(A,b)

% Gaussova eliminacni metoda pro reseni A*u = b

n = length(b);

% Dopredna eliminace
for i=1:n-1

    if A(i,i)==0
        for j=i+1:n
            if A(j,i)~=0
                tmp = A(i,:); A(i,:) = A(j,:); A(j,:) = tmp;
                tmp = b(i); b(i) = b(j); b(j) = tmp;
                break;
            end
        end
    end
    
    for j=i+1:n
        factor = A(j,i)/A(i,i);
        for k=i:n
            A(j,k) = A(j,k) - factor*A(i,k);
        end
        b(j) = b(j) - factor*b(i);
    end
    
end


% Zpetna eliminace
for i=n:-1:1
    
    u(i) = b(i)/A(i,i);
    
    for j=1:i-1
        b(j) = b(j) - A(j,i)*u(i);
        A(j,i) = 0;
    end
    
end
