2021-03-06 18:17:36 +01:00
|
|
|
function A = gaussian_elimination_with_complete_pivoting(A)
|
2021-03-06 15:54:10 +01:00
|
|
|
|
|
|
|
% det(A(mi, lm))
|
|
|
|
|
|
|
|
[n, m] = size(A);
|
|
|
|
if n ~= m
|
|
|
|
error('Matrix is not squared!')
|
|
|
|
end
|
|
|
|
|
|
|
|
if det(A) == 0
|
2021-03-06 18:17:36 +01:00
|
|
|
error('Matrix is not nonsingular!')
|
2021-03-06 15:54:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
for k = 1 : n-1
|
|
|
|
i = k:n;
|
|
|
|
j = k:n;
|
|
|
|
A(i, j);
|
2021-03-06 18:17:36 +01:00
|
|
|
maximum = max(abs(A(i, j)), [], 'all');
|
|
|
|
max_idx = find(abs(A==maximum));
|
|
|
|
[mi, lm] = ind2sub(size(A), max_idx(1));
|
|
|
|
A([k mi], 1:n) = deal(A([mi k], 1:n));
|
|
|
|
A(1:n, [k lm]) = deal(A(1:n, [lm k]));
|
|
|
|
p(k) = mi;
|
|
|
|
q(k) = lm;
|
|
|
|
% Perform Gaussian elimination with the greatest pivot
|
2021-03-06 15:54:10 +01:00
|
|
|
if A(k, k) ~= 0
|
2021-03-06 18:17:36 +01:00
|
|
|
rows = k+1 : n;
|
|
|
|
A(rows, k) = A(rows, k)/A(k, k);
|
|
|
|
A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows);
|
2021-03-06 15:54:10 +01:00
|
|
|
end
|
2021-03-06 18:17:36 +01:00
|
|
|
end
|