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

21 lines
363 B
Mathematica
Raw Normal View History

2021-03-13 13:42:56 +01:00
function A = Alg5_gauss_jordan_elimination(A)
2021-03-13 16:16:01 +01:00
% Algorithm 5: Gauss-Jordan Elimination
% Argument A is an augmented matrix
2021-03-06 20:58:52 +01:00
2021-03-13 13:33:13 +01:00
% M rows, N columns
2021-03-13 13:33:13 +01:00
[M, N] = size(A);
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
2021-03-13 16:16:01 +01:00
end
2021-03-06 20:58:52 +01:00
end