function x=Full_Pivoting(A,b)
%This code is to solve the linear function by FULL PIVOTING Gaussian
%Elimination method.   
%   x=Full_Pivoting(InputMatrix, b).
%   To solve InputMatrix*x=b
%   InputMatrix = the N*N dimension matrix. A verification of the input
%   matrix is contained in this code. If the input matrix is singlur, the
%   code will remind you. But user must keep InputMatrix a N*N matrix. If
%   it's not, there would be error.
%
%   b = the right side of the linear system: Ax=b. The input b must be a
%   N*1 vector. If it's not, the code will be error.
%
%   x = the output. x is the solution of the linear system. x is given by a
%   N*1 vector.

    N=length(A);my_sort=1:1:N;x=zeros(N,1);
    for i=1:N
        if max(max(abs(A(i:N,i:N))))<10e-6
            disp('the input matrix is nearly singlur, so the solution maybe weird. If you are the grader, please verify your input!!!');
        end
        [hang lie]=find(abs(A(i:N,i:N))==max(max(abs(A(i:N,i:N)))));
        hang(1)=hang(1)+(i-1);lie(1)=lie(1)+(i-1);
        x1=A(i,:);A(i,:)=A(hang(1),:);A(hang(1),:)=x1;
        y1=A(:,i);A(:,i)=A(:,lie(1));A(:,lie(1))=y1;
        z1=my_sort(i);my_sort(i)=my_sort(lie(1));my_sort(lie(1))=z1;
        z1=b(i);b(i)=b(hang(1));b(hang(1))=z1;
        for j=(i+1):N
            b(j)=b(j)-b(i)/A(i,i)*A(j,i);
            A(j,i:N)=A(j,i:N)-A(i,i:N)/A(i,i)*A(j,i);
        end
    end
    for i=N:(-1):1
        s=0;
        for j=(i+1):N
            s=s+A(i,j)*x(my_sort(j));
        end
        x(my_sort(i))=(b(i)-s)/A(i,i);
    end
end

