function [x,t,X,T] = Khama4610hw5(A,b);

%This function will check to see if A and b are compatible and then 
%return x solved by Matlab, X solved by row reduction, time required
%by the computer to solve the the system by Matlab, and time required by the computer to 
%solve the the system by row reduction.  This function will also use partial pivoting to solve the system.   

%Determines sizes of matrix A and b.
[m, n] = size(A);
[o, p] = size(b);

%Checks whether Matrix A and b are compatible.
if n ~= o
    'Matrix A and vector b are not compatible because inner dimensions are not equal.  Matrix A is m x n',m,n,'and matrix b is o x p.',o,p,;  
    break;
end

%Checks whether Matrix A is square or not.
if m ~= n
    'Matrix A is not a square matrix.  Matix A is m x n.',m,n,;  
    break;
end

%Solves the system A x = b using Matlab and returns x and time required.
tic
x = A\b;
t = toc;

%Solves the system A x = b using the row reduction and returns X and time required.
tic

%Creates a matrix H and sets it equal to A and creates a column equal to b.
H = A;
H(:,n+1)= b;

%For loop to row reduce the lower left corner of the matrix H (i and j are just counters).
for i = 1:n
    
    %Checks if a non zero pivot is ever encountered.
    if H(i,i) == 0
        'Non zero pivot.';
        break;
    end
   
    %Performs partial pivoting by sorting rows in descending order.
    H(i:n,:) = sortrows(H(i:n,:));
    if abs(H(i,i)) < abs(H(i,n))
       C(n,:) = H(i,:);
       H(i,:) = H(n,:);
       H(n,:) = C(n,:);
    end
    
    H(i,:) = H(i,:)/H(i,i);
    for j =1:n-i
        H(i+j,:)= H(i,:)*H(i+j,i) - H(i+j,:);
    end
end

%For loop to row reduce the upper right corner of the matrix H (i and j are just counters).
for j = 1:n
    for i = 1:n-j
        H(n-i+1-j,:) = H(n-i+1-j,:) - H(n+1-j,:)*H(n-i+1-j,n+1-j);
    end
end

T = toc;
X = H(:,n+1);

