From 41b7057eabc827264881ba05111353abb36d4fdd Mon Sep 17 00:00:00 2001 From: EdwardEisenhauer Date: Sat, 13 Mar 2021 13:42:56 +0100 Subject: [PATCH] Refactored functions from 1 to 5 --- ...n.m => Alg1_outer_product_gaussian_elimination.m} | 6 ++++-- ...g2_gaussian_elimination_with_complete_pivoting.m} | 2 +- ...rd_substitution.m => Alg3_forward_substitution.m} | 2 +- ...{back_substitution.m => Alg4_back_substitution.m} | 2 +- ...elimination.m => Alg5_gauss_jordan_elimination.m} | 4 ++-- Direct Methods for Solving Linear Systems/main.m | 12 +++++++----- 6 files changed, 16 insertions(+), 12 deletions(-) rename Direct Methods for Solving Linear Systems/{outer_product_gaussian_elimination.m => Alg1_outer_product_gaussian_elimination.m} (83%) rename Direct Methods for Solving Linear Systems/{gaussian_elimination_with_complete_pivoting.m => Alg2_gaussian_elimination_with_complete_pivoting.m} (92%) rename Direct Methods for Solving Linear Systems/{forward_substitution.m => Alg3_forward_substitution.m} (86%) rename Direct Methods for Solving Linear Systems/{back_substitution.m => Alg4_back_substitution.m} (90%) rename Direct Methods for Solving Linear Systems/{gauss_jordan_elimination.m => Alg5_gauss_jordan_elimination.m} (81%) diff --git a/Direct Methods for Solving Linear Systems/outer_product_gaussian_elimination.m b/Direct Methods for Solving Linear Systems/Alg1_outer_product_gaussian_elimination.m similarity index 83% rename from Direct Methods for Solving Linear Systems/outer_product_gaussian_elimination.m rename to Direct Methods for Solving Linear Systems/Alg1_outer_product_gaussian_elimination.m index 8d43864..64dd6f7 100644 --- a/Direct Methods for Solving Linear Systems/outer_product_gaussian_elimination.m +++ b/Direct Methods for Solving Linear Systems/Alg1_outer_product_gaussian_elimination.m @@ -1,5 +1,5 @@ % Algorithm 1: Outer Product Gaussian Elimination (Alg. 3.2.1) -function A = outer_product_gaussian_elimination(A) +function A = Alg1_outer_product_gaussian_elimination(A) [n, m] = size(A); if n ~= m @@ -14,4 +14,6 @@ end 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 \ No newline at end of file + 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/Alg2_gaussian_elimination_with_complete_pivoting.m similarity index 92% rename from Direct Methods for Solving Linear Systems/gaussian_elimination_with_complete_pivoting.m rename to Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m index 521d913..c760e74 100644 --- a/Direct Methods for Solving Linear Systems/gaussian_elimination_with_complete_pivoting.m +++ b/Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m @@ -1,4 +1,4 @@ -function [P, Q, L, U] = gaussian_elimination_with_complete_pivoting(A) +function [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A) [n, m] = size(A); if n ~= m diff --git a/Direct Methods for Solving Linear Systems/forward_substitution.m b/Direct Methods for Solving Linear Systems/Alg3_forward_substitution.m similarity index 86% rename from Direct Methods for Solving Linear Systems/forward_substitution.m rename to Direct Methods for Solving Linear Systems/Alg3_forward_substitution.m index 591e6d0..84a0acb 100644 --- a/Direct Methods for Solving Linear Systems/forward_substitution.m +++ b/Direct Methods for Solving Linear Systems/Alg3_forward_substitution.m @@ -1,5 +1,5 @@ % Algorithm 3: Forward Substitution (Alg. 3.1.1) -function b = forward_substitution(L, b) +function b = Alg3_forward_substitution(L, b) [n, m] = size(L); if n ~= m diff --git a/Direct Methods for Solving Linear Systems/back_substitution.m b/Direct Methods for Solving Linear Systems/Alg4_back_substitution.m similarity index 90% rename from Direct Methods for Solving Linear Systems/back_substitution.m rename to Direct Methods for Solving Linear Systems/Alg4_back_substitution.m index 0861344..f994f62 100644 --- a/Direct Methods for Solving Linear Systems/back_substitution.m +++ b/Direct Methods for Solving Linear Systems/Alg4_back_substitution.m @@ -1,5 +1,5 @@ % Argorithm 4: Back Substitution (Alg. 3.1.2) -function b = back_substitution(U,b) +function b = Alg4_back_substitution(U,b) [n, m] = size(U); if n ~= m diff --git a/Direct Methods for Solving Linear Systems/gauss_jordan_elimination.m b/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m similarity index 81% rename from Direct Methods for Solving Linear Systems/gauss_jordan_elimination.m rename to Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m index 09c63bc..2985500 100644 --- a/Direct Methods for Solving Linear Systems/gauss_jordan_elimination.m +++ b/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m @@ -1,6 +1,6 @@ -% Algorithm 5: Gauss-Jordan Elimination +% Algorithm 5: Gauss-Jordan Elimination (Alg. % Argument A is an augmented matrix -function A = gauss_jordan_elimination(A) +function A = Alg5_gauss_jordan_elimination(A) % M – rows, N – columns diff --git a/Direct Methods for Solving Linear Systems/main.m b/Direct Methods for Solving Linear Systems/main.m index 129d201..b0a94f5 100644 --- a/Direct Methods for Solving Linear Systems/main.m +++ b/Direct Methods for Solving Linear Systems/main.m @@ -7,7 +7,7 @@ A = [2, -1, 0, 0; b = [0;0;0;5]; -B = outer_product_gaussian_elimination(A); +B = Alg1_outer_product_gaussian_elimination(A); U = triu(B); L = tril(B, -1); x = back_substitution(U, b); @@ -23,7 +23,7 @@ b = [0;0;0;5]; B = gauss_jordan_elimination([A b]) -[P, Q, L, U] = gaussian_elimination_with_complete_pivoting(A); +[P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A); b = P*b; % Ly = b and Ux = y @@ -40,7 +40,7 @@ A = [1, 1, 1; b = [1;2;1]; -[P, Q, L, U] = gaussian_elimination_with_complete_pivoting(A); +[P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A); b = P*b; % Ly = b and Ux = y @@ -69,11 +69,13 @@ A = [2, 1, 2; 1, 2, 3; 4, 1, 2]; -[P, Q, L, U] = gaussian_elimination_with_complete_pivoting(A); +[P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A); I = P*eye(3); % Ly = b and Ux = y y = forward_substitution(L, I); X = Q*back_substitution(U, y) -inv(A) \ No newline at end of file +inv(A) + +%% Problem 6