From 7742a178b7c50bf51a05c271594098948ac77232 Mon Sep 17 00:00:00 2001 From: EdwardEisenhauer Date: Sat, 6 Mar 2021 15:54:10 +0100 Subject: [PATCH] Fixed Algs 1 and 4 and introduced gaussian_elimination_with_complete_pivoting --- .gitignore | 1 + .../back_substitution.m | 9 +++--- ...ssian_elimination_with_complete_pivoting.m | 30 +++++++++++++++++++ .../outer_product_gaussian_elimination.m | 8 ++--- 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 Direct Methods for Solving Linear Systems/gaussian_elimination_with_complete_pivoting.m diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e3461a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.asv diff --git a/Direct Methods for Solving Linear Systems/back_substitution.m b/Direct Methods for Solving Linear Systems/back_substitution.m index 2d1a1f0..bbe1a07 100644 --- a/Direct Methods for Solving Linear Systems/back_substitution.m +++ b/Direct Methods for Solving Linear Systems/back_substitution.m @@ -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 - error('Matrix is not nonsingular!') +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 \ No newline at end of file diff --git a/Direct Methods for Solving Linear Systems/gaussian_elimination_with_complete_pivoting.m b/Direct Methods for Solving Linear Systems/gaussian_elimination_with_complete_pivoting.m new file mode 100644 index 0000000..b835b52 --- /dev/null +++ b/Direct Methods for Solving Linear Systems/gaussian_elimination_with_complete_pivoting.m @@ -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 \ No newline at end of file diff --git a/Direct Methods for Solving Linear Systems/outer_product_gaussian_elimination.m b/Direct Methods for Solving Linear Systems/outer_product_gaussian_elimination.m index 740deec..83a811b 100644 --- a/Direct Methods for Solving Linear Systems/outer_product_gaussian_elimination.m +++ b/Direct Methods for Solving Linear Systems/outer_product_gaussian_elimination.m @@ -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)