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

28 lines
490 B
Mathematica
Raw Normal View History

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