Fixed Algs 1 and 4 and introduced gaussian_elimination_with_complete_pivoting

This commit is contained in:
Sergiusz Warga 2021-03-06 15:54:10 +01:00
parent 2b47852870
commit 7742a178b7
4 changed files with 40 additions and 8 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.asv

View File

@ -1,3 +1,4 @@
% Argorithm 4: Back Substitution (Alg. 3.1.2)
function back_substitution(U,b)
[n, m] = size(U);
@ -9,13 +10,13 @@ if length(b) ~= n
error('Vector b has wrong length!')
end
if det(A) == 0
if det(U) == 0
error('Matrix is not nonsingular!')
end
b(n) = b(n)/U(n, n)
for i = n-1:-1:n
for i = n-1:-1:1
b(i) = (b(i) - U(i, i+1 : n)*b(i+1 : n))/U(i, i)
end
end

View File

@ -0,0 +1,30 @@
function gaussian_elimination_with_complete_pivoting(A)
% det(A(mi, lm))
[n, m] = size(A);
if n ~= m
error('Matrix is not squared!')
end
if det(A) == 0
error('Matrix is not nonsingular!')
end
for k = 1 : n-1
i = k:n;
j = k:n;
A(i, j);
maximum = max(abs(A(i, j)), [], 'all')
max_idx = find(abs(A==maximum))
[mi, lm] = ind2sub(size(A), max_idx(1))
A(k, 1:n) = A(mi, 1:n)
A(1:n, k) = A(1:n, lm)
p(k) = mi
q(k) = lm
if A(k, k) ~= 0
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)
end
end

View File

@ -1,5 +1,5 @@
% Outer Product Gaussian Elimination (Alg. 3.2.1)
function U, Mk = outer_product_gaussian_elimination(A)
% Algorithm 1: Outer Product Gaussian Elimination (Alg. 3.2.1)
function [U, Mk] = outer_product_gaussian_elimination(A)
[n, m] = size(A);
if n ~= m
@ -10,13 +10,13 @@ end
error('Matrix is not nonsingular!')
end
A
A;
for k = 1 : n-1
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);
A
A;
end
U = triu(A)