mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 17:43:12 +02:00
Task 2 code
This commit is contained in:
parent
5c984d09a7
commit
d5089c4a9e
@ -1,15 +1,37 @@
|
||||
function x = jacobiMethod(Matrix, Vector)
|
||||
[Rows,~] = size(Matrix);
|
||||
[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);
|
||||
[L, D, U] = decomposeMatrix(Matrix);
|
||||
initial_x = ones(Rows, 1);
|
||||
whichIterationAreWeOn = 0;
|
||||
currentError = inf; % We set it to inf so that the algorithm will always start
|
||||
% (See condition below)
|
||||
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
|
||||
|
||||
x = jacobiEquation(D, L, U, initial_x, Vector);
|
||||
|
||||
currentError = norm(x - initial_x);
|
||||
%disp(currentError);
|
||||
if currentError <= demandedTolerance
|
||||
@ -24,22 +46,6 @@ function x = jacobiMethod(Matrix, Vector)
|
||||
initial_x = x;
|
||||
whichIterationAreWeOn = whichIterationAreWeOn + 1;
|
||||
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
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user