AAE-NA-Labs/Direct Methods for Solving Linear Systems/gauss_jordan_elimination.m

25 lines
447 B
Mathematica
Raw Normal View History

% Algorithm 5: Gauss-Jordan Elimination
% Input A is an augmented matrix
2021-03-06 20:58:52 +01:00
function A = gauss_jordan_elimination(A)
[n, m] = size(A);
if n + 1 ~= m
2021-03-06 20:58:52 +01:00
error('Matrix is not squared!')
end
% if det(A) == 0
% error('Matrix is not nonsingular!')
% end
2021-03-06 20:58:52 +01:00
for k = 1 : m-1
row = A(k, :);
row = row/row(k);
A(k, :) = row;
for l = 1 : m-1
if l ~= k
A(l, :) = A(l, :)-(A(l, k))*row;
end
2021-03-06 20:58:52 +01:00
end
end