Refactored functions from 1 to 5

This commit is contained in:
Sergiusz Warga 2021-03-13 13:42:56 +01:00
parent 302dc37f78
commit 41b7057eab
6 changed files with 16 additions and 12 deletions

View File

@ -1,5 +1,5 @@
% Algorithm 1: Outer Product Gaussian Elimination (Alg. 3.2.1) % 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); [n, m] = size(A);
if n ~= m if n ~= m
@ -14,4 +14,6 @@ end
rows = k + 1 : n; rows = k + 1 : n;
A(rows, k) = A(rows, k)/A(k, k); A(rows, k) = A(rows, k)/A(k, k);
A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows); A(rows, rows) = A(rows, rows) - A(rows, k) * A(k, rows);
end end
end

View File

@ -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); [n, m] = size(A);
if n ~= m if n ~= m

View File

@ -1,5 +1,5 @@
% Algorithm 3: Forward Substitution (Alg. 3.1.1) % 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); [n, m] = size(L);
if n ~= m if n ~= m

View File

@ -1,5 +1,5 @@
% Argorithm 4: Back Substitution (Alg. 3.1.2) % 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); [n, m] = size(U);
if n ~= m if n ~= m

View File

@ -1,6 +1,6 @@
% Algorithm 5: Gauss-Jordan Elimination % Algorithm 5: Gauss-Jordan Elimination (Alg.
% Argument A is an augmented matrix % Argument A is an augmented matrix
function A = gauss_jordan_elimination(A) function A = Alg5_gauss_jordan_elimination(A)
% M rows, N columns % M rows, N columns

View File

@ -7,7 +7,7 @@ A = [2, -1, 0, 0;
b = [0;0;0;5]; b = [0;0;0;5];
B = outer_product_gaussian_elimination(A); B = Alg1_outer_product_gaussian_elimination(A);
U = triu(B); U = triu(B);
L = tril(B, -1); L = tril(B, -1);
x = back_substitution(U, b); x = back_substitution(U, b);
@ -23,7 +23,7 @@ b = [0;0;0;5];
B = gauss_jordan_elimination([A b]) 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; b = P*b;
% Ly = b and Ux = y % Ly = b and Ux = y
@ -40,7 +40,7 @@ A = [1, 1, 1;
b = [1;2;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; b = P*b;
% Ly = b and Ux = y % Ly = b and Ux = y
@ -69,11 +69,13 @@ A = [2, 1, 2;
1, 2, 3; 1, 2, 3;
4, 1, 2]; 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); I = P*eye(3);
% Ly = b and Ux = y % Ly = b and Ux = y
y = forward_substitution(L, I); y = forward_substitution(L, I);
X = Q*back_substitution(U, y) X = Q*back_substitution(U, y)
inv(A) inv(A)
%% Problem 6