So tired
This commit is contained in:
parent
87f957d903
commit
7809b5d87b
@ -1,6 +1,4 @@
|
|||||||
function A = gaussian_elimination_with_complete_pivoting(A)
|
function [U, L] = gaussian_elimination_with_complete_pivoting(A)
|
||||||
|
|
||||||
% det(A(mi, lm))
|
|
||||||
|
|
||||||
[n, m] = size(A);
|
[n, m] = size(A);
|
||||||
if n ~= m
|
if n ~= m
|
||||||
@ -11,21 +9,35 @@ if det(A) == 0
|
|||||||
error('Matrix is not nonsingular!')
|
error('Matrix is not nonsingular!')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
A
|
||||||
|
|
||||||
|
% for k = 1 : n-1
|
||||||
for k = 1 : n-1
|
for k = 1 : n-1
|
||||||
i = k:n;
|
i = k:n;
|
||||||
j = k:n;
|
j = k:n;
|
||||||
A(i, j);
|
A(i, j)
|
||||||
maximum = max(abs(A(i, j)), [], 'all');
|
[max_val, rows_of_max_in_col] = max(abs(A(i, j)));
|
||||||
max_idx = find(abs(A==maximum));
|
[max_val, max_col] = max(max_val);
|
||||||
[mi, lm] = ind2sub(size(A), max_idx(1));
|
max_row = rows_of_max_in_col(max_col);
|
||||||
A([k mi], 1:n) = deal(A([mi k], 1:n));
|
% Assing value of mi and lambda in respect to the main A matrix
|
||||||
A(1:n, [k lm]) = deal(A(1:n, [lm k]));
|
[mi, lm] = deal(max_row+k-1, max_col+k-1)
|
||||||
p(k) = mi;
|
A([k mi], 1:n) = deal(A([mi k], 1:n))
|
||||||
q(k) = lm;
|
A(1:n, [k lm]) = deal(A(1:n, [lm k]))
|
||||||
|
p(k) = mi
|
||||||
|
q(k) = lm
|
||||||
% Perform Gaussian elimination with the greatest pivot
|
% Perform Gaussian elimination with the greatest pivot
|
||||||
|
|
||||||
if A(k, k) ~= 0
|
if A(k, k) ~= 0
|
||||||
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
|
end
|
||||||
|
|
||||||
|
U = triu(A);
|
||||||
|
L = tril(A, -1) + eye(n);
|
||||||
|
p
|
||||||
|
I = eye(n);
|
||||||
|
P = I(p, :)
|
||||||
|
q
|
||||||
|
Q = I(:, q)
|
@ -1,10 +1,15 @@
|
|||||||
clear all;
|
clear all;
|
||||||
|
|
||||||
B = [2, -1, 0, 0; -1, 2, -1, 0;0, -1, 2, -1; 0, 0, -1, 2];
|
B = [2, -1, 0, 0;
|
||||||
|
-1, 2, -1, 0;
|
||||||
|
3, -1, 2, -1;
|
||||||
|
0, 4, -1, 2];
|
||||||
|
|
||||||
b = [0;0;0;5];
|
b = [0;0;0;5];
|
||||||
|
|
||||||
[U, Mk] = outer_product_gaussian_elimination(B);
|
[U, L] = outer_product_gaussian_elimination(B);
|
||||||
back_substitution(U, b);
|
back_substitution(U, b);
|
||||||
|
|
||||||
A = gaussian_elimination_with_complete_pivoting(B);
|
[U, L] = gaussian_elimination_with_complete_pivoting(B)
|
||||||
|
L*U
|
||||||
|
% A = gauss_jordan_elimination(B)
|
@ -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 [U, Mk] = outer_product_gaussian_elimination(A)
|
function [U, L] = outer_product_gaussian_elimination(A)
|
||||||
|
|
||||||
[n, m] = size(A);
|
[n, m] = size(A);
|
||||||
if n ~= m
|
if n ~= m
|
||||||
@ -17,4 +17,4 @@ end
|
|||||||
end
|
end
|
||||||
|
|
||||||
U = triu(A);
|
U = triu(A);
|
||||||
Mk = diag(A,-1); % Gauss vector
|
L = tril(A, -1) + eye(n);
|
||||||
|
Loading…
Reference in New Issue
Block a user