From be23b626db8a0657004c960ae274575b2549ab4a Mon Sep 17 00:00:00 2001 From: EdwardEisenhauer Date: Sat, 13 Mar 2021 16:16:01 +0100 Subject: [PATCH] Added shitty Algorithm 6 --- ...ssian_elimination_with_complete_pivoting.m | 1 - .../Alg5_gauss_jordan_elimination.m | 13 ++--- .../Alg6_RREF.m | 51 +++++++++++++++++++ .../main.m | 19 ++++++- 4 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 Direct Methods for Solving Linear Systems/Alg6_RREF.m diff --git a/Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m b/Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m index c760e74..172bc67 100644 --- a/Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m +++ b/Direct Methods for Solving Linear Systems/Alg2_gaussian_elimination_with_complete_pivoting.m @@ -16,7 +16,6 @@ q = 1:n; for k = 1 : n-1 i = k:n; j = k:n; - A(i, j); [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); diff --git a/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m b/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m index 2985500..a48b804 100644 --- a/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m +++ b/Direct Methods for Solving Linear Systems/Alg5_gauss_jordan_elimination.m @@ -1,18 +1,10 @@ -% Algorithm 5: Gauss-Jordan Elimination (Alg. -% Argument A is an augmented matrix function A = Alg5_gauss_jordan_elimination(A) +% Algorithm 5: Gauss-Jordan Elimination +% Argument A is an augmented matrix % M – rows, N – columns [M, N] = size(A); -% -% if M + 1 ~= N -% error('Matrix is not squared!') -% end -% -% if det(A) == 0 -% error('Matrix is not nonsingular!') -% end for m = 1 : M @@ -25,4 +17,5 @@ for m = 1 : M A(n, :) = A(n, :)-(A(n, m))*row; end end +end end \ No newline at end of file diff --git a/Direct Methods for Solving Linear Systems/Alg6_RREF.m b/Direct Methods for Solving Linear Systems/Alg6_RREF.m new file mode 100644 index 0000000..01314d1 --- /dev/null +++ b/Direct Methods for Solving Linear Systems/Alg6_RREF.m @@ -0,0 +1,51 @@ +function A = Alg6_RREF(A) +% Algorithm 6: Reduced Row Echelon Form (RREF) + +% M – rows, N – columns + +[M, N] = size(A); + +n = 0; + +for m = 1 : M + n = n + 1 + if n > N + break + end + A + % We want the left-most coefficient to be 1 (pivot) + row = A(m, :); + if row(m) == 0 + n = n + 1 + end + [m ,n] + row = row/row(n); + A(m, :) = row; + + for i = 1 : M + if i ~= m + A(i, :) = A(i, :)-(A(i, n))*row; + end + end + + A + + for i = m + 1 : M + A(i:end, m+1:end); % Partial matrix (in which we are looking for non-zero pivots) + A(i:end, m+1); % Left-most column + if ~any(A(i:end, m+1)) % If the left-most column has only zeros check the next one + m = m + 1; + end + A(i:end, m+1:end); + if A(i, m+1) == 0 + non_zero_row = find(A(i:end,m+1), 1); + if isempty(non_zero_row) + continue + end + A([i, i+non_zero_row-1], :) = deal(A([i+non_zero_row-1, i], :)); + end + end +end + +end + diff --git a/Direct Methods for Solving Linear Systems/main.m b/Direct Methods for Solving Linear Systems/main.m index b0a94f5..243cd7f 100644 --- a/Direct Methods for Solving Linear Systems/main.m +++ b/Direct Methods for Solving Linear Systems/main.m @@ -60,8 +60,8 @@ bp = [0.168; 0.066]; kappa = cond(A) -B = gauss_jordan_elimination([A b]) -Bp = gauss_jordan_elimination([A bp]) +B = Alg5_gauss_jordan_elimination([A b]) +Bp = Alg5_gauss_jordan_elimination([A bp]) %% Problem 5 % AX = I3 @@ -79,3 +79,18 @@ X = Q*back_substitution(U, y) inv(A) %% Problem 6 + +%% Problem 10 + +% A = [1 2 2 3 1; +% 2 4 4 6 2; +% 3 6 6 9 6; +% 1 2 4 5 3] + +A = [0.835, 0.667; + 0.333, 0.266]; +b = [0.168; 0.067]; + +Alg6_RREF([A b]) + +