function x = naivegauss(A,b)
% Function to demonstrate the Naive Gauss elimination method
% without pivoting
%
% A is nxn matrix of coefficients
% b is nx1 column vector of constants
% x is the solution (nx1 column) to x = A*b
%
% Syntax: x = naivegauss(A,b)

clc

[rA,cA] = size(A);  % Determine the size of A
[rb,cb] = size(b);  % Determine the size of b

% Input checks
if rA ~= cA
    disp('Error: A is not a square matrix')
    return
else
    n = rA;
end
if cb ~= 1
    disp('Error: b is not a column vector')
    return
end
if cA ~= rb
    disp('Error: Matrix and vector dimensions are not compatible')
    return
end
dA = det(A);
if dA == 0
    disp('Error: Matrix is singular')
    return
end

% Initialize the result x
x = zeros(n,1); % initialize x as a col

% Step 1: Form the combined matrix [A b]
a = [A b];
disp(' ')
disp('Combined Matrix [A|b]')
disp('-----------------------------')
disp(a)
disp(' ')

% Step 2: Forward elimination
disp('-----------------------------')
disp('Forward Elimination')
disp('-----------------------------')
disp(' ')
for k = 1:n-1
    disp(['Pivot Row ',num2str(k)])
    disp('-----------------------------')
        
    for i = k+1:n
        m = a(i,k)/a(k,k);
        for j = k:n+1
            a(i,j)=a(i,j)-m*a(k,j);
        end
    end
    disp(a)
    disp(' ')   
end

% Step 3: Backward substitution
disp('-----------------------------')
disp('Backward Substitution')
disp('-----------------------------')
disp(' ')
x(n)=a(n,n+1)/a(n,n);
for i = n:-1:1
    sum = 0;
    for j = i+1:n
        sum = sum + a(i,j)*x(j);
    end
    x(i) = (a(i,n+1)-sum)/a(i,i);
end
disp(x)
