diff --git a/Direct Methods for Solving Linear Systems/Alg1_outer_product_gaussian_elimination.m b/Direct Methods for Solving Linear Systems/Alg1.m similarity index 89% rename from Direct Methods for Solving Linear Systems/Alg1_outer_product_gaussian_elimination.m rename to Direct Methods for Solving Linear Systems/Alg1.m index babce75..2c66149 100644 --- a/Direct Methods for Solving Linear Systems/Alg1_outer_product_gaussian_elimination.m +++ b/Direct Methods for Solving Linear Systems/Alg1.m @@ -1,4 +1,4 @@ -function A = Alg1_outer_product_gaussian_elimination(A) +function A = Alg1(A) % Algorithm 1: Outer Product Gaussian Elimination % Performs a gaussian eliminaion on a square matrix A. @@ -22,4 +22,5 @@ for k = 1 : m-1 A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows); end -end \ No newline at end of file +end + diff --git a/Direct Methods for Solving Linear Systems/Alg11.m b/Direct Methods for Solving Linear Systems/Alg11.m index b33c7c4..ce72d4f 100644 --- a/Direct Methods for Solving Linear Systems/Alg11.m +++ b/Direct Methods for Solving Linear Systems/Alg11.m @@ -1,5 +1,5 @@ function [Q R] = Alg11(A) -% Algorithm 11: QR factorization via Householder algorithm. +% Algorithm 11: QR factorization via Householder transformation. [m, n] = size(A); diff --git a/Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m b/Direct Methods for Solving Linear Systems/Alg2.m similarity index 94% rename from Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m rename to Direct Methods for Solving Linear Systems/Alg2.m index 43f9cfe..bb2ac58 100644 --- a/Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m +++ b/Direct Methods for Solving Linear Systems/Alg2.m @@ -1,4 +1,4 @@ -function [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A) +function [P, Q, L, U] = Alg2(A) % Algorithm 2: Gaussian Elimination with Complete Pivoting. % [P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A) % computes the complete pivoting factorization PAQ = LU. diff --git a/Direct Methods for Solving Linear Systems/Alg3_forward_substitution.m b/Direct Methods for Solving Linear Systems/Alg3.m similarity index 92% rename from Direct Methods for Solving Linear Systems/Alg3_forward_substitution.m rename to Direct Methods for Solving Linear Systems/Alg3.m index b793584..17af4b5 100644 --- a/Direct Methods for Solving Linear Systems/Alg3_forward_substitution.m +++ b/Direct Methods for Solving Linear Systems/Alg3.m @@ -1,4 +1,4 @@ -function b = Alg3_forward_substitution(L, b) +function b = Al3(L, b) % Algorithm 3: Forward Substitution % b = Alg3_forward_substitution(L, b) overwrites b with the solution to % Lx = b. diff --git a/Direct Methods for Solving Linear Systems/Alg4_back_substitution.m b/Direct Methods for Solving Linear Systems/Alg4.m similarity index 84% rename from Direct Methods for Solving Linear Systems/Alg4_back_substitution.m rename to Direct Methods for Solving Linear Systems/Alg4.m index ca4185e..5a6d538 100644 --- a/Direct Methods for Solving Linear Systems/Alg4_back_substitution.m +++ b/Direct Methods for Solving Linear Systems/Alg4.m @@ -1,4 +1,4 @@ -function b = Alg4_back_substitution(U,b) +function b = Alg4(U,b) % Argorithm 4: Back Substitution % b = Alg4_back_substitution(U,b) returns vetor b with solution to the % Ux = b. @@ -17,9 +17,9 @@ if length(b) ~= m error('Vector b has wrong length!') end -if det(U) < eps - error('Matrix is not nonsingular!') -end +% if det(U) < eps +% error('Matrix is not nonsingular!') +% end % The following algorithm is based on the Algrotihm 3.1.2 from [2]. diff --git a/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m b/Direct Methods for Solving Linear Systems/Alg5.m similarity index 79% rename from Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m rename to Direct Methods for Solving Linear Systems/Alg5.m index 28f598d..6c83199 100644 --- a/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m +++ b/Direct Methods for Solving Linear Systems/Alg5.m @@ -1,9 +1,9 @@ -function A = Alg5_gauss_jordan_elimination(A) +function A = Alg5(A) % Algorithm 5: Gauss-Jordan Elimination % A = Alg5_gauss_jordan_elimination(A) performs Gauss-Jordan elimination % on an augmented matrix A. -[m, n] = size(A); +[m, ~] = size(A); for k = 1 : m @@ -12,7 +12,7 @@ for k = 1 : m A(k, :) = row; for i = 1 : m - if i ~= k + if i ~= k && A(i, k) ~= 0 A(i, :) = A(i, :)-(A(i, k))*row; end end diff --git a/Direct Methods for Solving Linear Systems/main.m b/Direct Methods for Solving Linear Systems/main.m index 2ae813a..502ca22 100644 --- a/Direct Methods for Solving Linear Systems/main.m +++ b/Direct Methods for Solving Linear Systems/main.m @@ -1,54 +1,28 @@ -clear all; -clc; - -A = [2, -1, 0, 0; - -1, 2, -1, 0; - 0, -1, 2, -1; - 0, 0, -1, 2]; - -b = [0;0;0;5]; - -B = Alg1_outer_product_gaussian_elimination(A); -U = triu(B); -x = Alg4_back_substitution(U, b); - %% Problem 1 clear all; +clc; A = [2, -1, 0, 0; -1, 2, -1, 0; 0, -1, 2, -1; 0, 0, -1, 2]; - b = [0;0;0;5]; - -B = gauss_jordan_elimination([A b]) - -[P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A); - -b = P*b; -% Ly = b and Ux = y -y = forward_substitution(L, b); - -x = Q*back_substitution(U, y); - -% L*U +B = Alg5([A b]) %% Problem 2 +clear all; +clc; A = [1, 1, 1; 1, 1, 2; 1, 2, 2]; - b = [1;2;1]; -[P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A); +[P, Q, L, U] = Alg2(A); b = P*b; % Ly = b and Ux = y -y = Alg3_forward_substitution(L, b); +y = Alg3(L, b); % Forward substitution +x = Q*Alg4(U, y) % Backward substitution -x = Q*Alg4_back_substitution(U, y) - -% L*U %% Problem 4 @@ -60,25 +34,34 @@ bp = [0.168; 0.066]; kappa = cond(A) -B = Alg5_gauss_jordan_elimination([A b]) -Bp = Alg5_gauss_jordan_elimination([A bp]) +B = Alg5([A b]) +Bp = Alg5([A bp]) %% Problem 5 -% AX = I3 +clear all; +clc; A = [2, 1, 2; 1, 2, 3; 4, 1, 2]; -[P, Q, L, U] = Alg2_gaussian_elimination_with_complete_pivoting(A); +[P, Q, L, U] = Alg2(A) I = P*eye(3); % Ly = b and Ux = y -y = forward_substitution(L, I); - -X = Q*back_substitution(U, y) -inv(A) +y = Alg3(L, I); % Forward substitution +x = Q*Alg4(U, y) % Backward substitution +inv(A) - x %% Problem 6 +clc; + +A = [1 2 3 4; + -1 1 2 1; + 0 2 1 3; + 0 0 1 1]; + +[L, U, P] = Alg8(A) +det(A) - prod(diag(U)) %% Problem 10