2021-03-07 22:56:18 +01:00
|
|
|
|
% 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-07 22:56:18 +01:00
|
|
|
|
|
2021-03-13 13:33:13 +01:00
|
|
|
|
[M, N] = size(A);
|
|
|
|
|
%
|
|
|
|
|
% if M + 1 ~= N
|
|
|
|
|
% error('Matrix is not squared!')
|
|
|
|
|
% end
|
|
|
|
|
%
|
2021-03-07 22:56:18 +01:00
|
|
|
|
% 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-07 22:56:18 +01:00
|
|
|
|
|
2021-03-13 13:33:13 +01:00
|
|
|
|
for n = 1 : M
|
|
|
|
|
if n ~= m
|
|
|
|
|
A(n, :) = A(n, :)-(A(n, m))*row;
|
2021-03-07 22:56:18 +01:00
|
|
|
|
end
|
2021-03-06 20:58:52 +01:00
|
|
|
|
end
|
|
|
|
|
end
|