2021-03-13 13:42:56 +01:00
|
|
|
function [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A)
|
2021-03-13 17:46:26 +01:00
|
|
|
% ADDME Algorithm 2: Gaussian Elimination with Complete Pivoting.
|
|
|
|
% [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A)
|
2021-03-06 15:54:10 +01:00
|
|
|
|
|
|
|
[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
|
|
|
|
|
2021-03-07 22:56:18 +01:00
|
|
|
p = 1:n;
|
|
|
|
q = 1:n;
|
2021-03-06 20:58:15 +01:00
|
|
|
|
|
|
|
% for k = 1 : n-1
|
2021-03-06 15:54:10 +01:00
|
|
|
for k = 1 : n-1
|
|
|
|
i = k:n;
|
|
|
|
j = k:n;
|
2021-03-06 20:58:15 +01:00
|
|
|
[max_val, rows_of_max_in_col] = max(abs(A(i, j)));
|
|
|
|
[max_val, max_col] = max(max_val);
|
|
|
|
max_row = rows_of_max_in_col(max_col);
|
|
|
|
% Assing value of mi and lambda in respect to the main A matrix
|
2021-03-07 22:56:18 +01:00
|
|
|
[mi, lm] = deal(max_row+k-1, max_col+k-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]) = p([mi, k]);
|
|
|
|
q([k, lm]) = q([lm, k]);
|
2021-03-06 20:58:15 +01:00
|
|
|
|
2021-03-07 22:56:18 +01:00
|
|
|
% 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
|
2021-03-06 20:58:15 +01:00
|
|
|
|
|
|
|
U = triu(A);
|
|
|
|
L = tril(A, -1) + eye(n);
|
|
|
|
I = eye(n);
|
2021-03-07 22:56:18 +01:00
|
|
|
P = I(p, :);
|
|
|
|
Q = I(:, q);
|