A = input("Enter matrix A: ");
B = input("Enter matrix B: ");

[mA, nA] = size(A);
[mB, nB] = size(B);
if mA ~= nA
    error("Matrix A must be a square matrix.");
elseif mA ~= mB || nB ~= 1
    error("Matrix B must have the same number of rows as matrix A with a single column.");
elseif det(A) == 0
    error("Determinate of matrix A must be non-zero.");
end

AB = [A, B];
for j = 1:nA
    for i = (j + 1):mA
        alpha = AB(i, j) / AB(j, j);
        AB(i, :) = AB(i, :) - alpha * AB(j, :);
    end
end

x = zeros(mA, 1);
for i = mA:-1:1
    x(i) = AB(i, nA + 1);
    for j = (i + 1):nA
        x(i) = x(i) - (AB(i, j) * x(j));
    end

    x(i) = x(i) / AB(i, i);
end

disp(x);