Refactored Direct Methods

This commit is contained in:
Sergiusz Warga 2021-03-21 19:45:18 +01:00
parent 37feb7c484
commit 8cfa2c9c18
7 changed files with 37 additions and 53 deletions

View File

@ -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
end

View File

@ -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);

View File

@ -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.

View File

@ -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.

View File

@ -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].

View File

@ -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

View File

@ -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