mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 14:03:16 +02:00
Task 2 code
This commit is contained in:
parent
5c984d09a7
commit
d5089c4a9e
@ -1,4 +1,15 @@
|
|||||||
function x = jacobiMethod(Matrix, Vector)
|
function x = jacobiMethod(Matrix, Vector)
|
||||||
|
[L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance] = initializeValues(Matrix)
|
||||||
|
x = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance, Vector);
|
||||||
|
disp("Final demandedTolerance");
|
||||||
|
disp(demandedTolerance);
|
||||||
|
disp("Final Iteration: ");
|
||||||
|
disp(whichIterationAreWeOn);
|
||||||
|
disp("A\b matlab:");
|
||||||
|
disp(Matrix \ Vector);
|
||||||
|
end
|
||||||
|
|
||||||
|
function [L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance] = initializeValues(Matrix)
|
||||||
[Rows, ~] = size(Matrix);
|
[Rows, ~] = size(Matrix);
|
||||||
[L, D, U] = decomposeMatrix(Matrix);
|
[L, D, U] = decomposeMatrix(Matrix);
|
||||||
initial_x = ones(Rows, 1);
|
initial_x = ones(Rows, 1);
|
||||||
@ -6,10 +17,21 @@ function x = jacobiMethod(Matrix, Vector)
|
|||||||
currentError = inf; % We set it to inf so that the algorithm will always start
|
currentError = inf; % We set it to inf so that the algorithm will always start
|
||||||
% (See condition below)
|
% (See condition below)
|
||||||
demandedTolerance = 1e-10;
|
demandedTolerance = 1e-10;
|
||||||
|
end
|
||||||
|
|
||||||
|
function [L, D, U] = decomposeMatrix(Matrix)
|
||||||
|
D = diag(diag(Matrix));
|
||||||
|
U = triu(Matrix, 1); % Generates upper triangular part of matrix
|
||||||
|
% where the second variable denotes on which diagonal of matrix should we
|
||||||
|
% start
|
||||||
|
L = tril(Matrix, -1); % Generates lower triangular part of matrix
|
||||||
|
% where the second variable denotes on which diagonal of matrix should we
|
||||||
|
% start
|
||||||
|
end
|
||||||
|
|
||||||
|
function x = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance, Vector)
|
||||||
while currentError >= demandedTolerance
|
while currentError >= demandedTolerance
|
||||||
|
|
||||||
x = jacobiEquation(D, L, U, initial_x, Vector);
|
x = jacobiEquation(D, L, U, initial_x, Vector);
|
||||||
|
|
||||||
currentError = norm(x - initial_x);
|
currentError = norm(x - initial_x);
|
||||||
%disp(currentError);
|
%disp(currentError);
|
||||||
if currentError <= demandedTolerance
|
if currentError <= demandedTolerance
|
||||||
@ -24,22 +46,6 @@ function x = jacobiMethod(Matrix, Vector)
|
|||||||
initial_x = x;
|
initial_x = x;
|
||||||
whichIterationAreWeOn = whichIterationAreWeOn + 1;
|
whichIterationAreWeOn = whichIterationAreWeOn + 1;
|
||||||
end
|
end
|
||||||
disp("Final demandedTolerance");
|
|
||||||
disp(demandedTolerance);
|
|
||||||
disp("Final Iteration: ");
|
|
||||||
disp(whichIterationAreWeOn);
|
|
||||||
disp("A\b matlab:");
|
|
||||||
disp(Matrix \ Vector);
|
|
||||||
end
|
|
||||||
|
|
||||||
function [L, D, U] = decomposeMatrix(Matrix)
|
|
||||||
D = diag(diag(Matrix));
|
|
||||||
U = triu(Matrix, 1); % Generates upper triangular part of matrix
|
|
||||||
% where the second variable denotes on which diagonal of matrix should we
|
|
||||||
% start
|
|
||||||
L = tril(Matrix, -1); % Generates lower triangular part of matrix
|
|
||||||
% where the second variable denotes on which diagonal of matrix should we
|
|
||||||
% start
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user