WUT_Computer_Science/ENUME/projectA/jacobiMethod.m

70 lines
3.0 KiB
Mathematica
Raw Normal View History

2021-11-11 19:19:57 +01:00
function x = jacobiMethod(Matrix, Vector)
2021-11-11 19:54:02 +01:00
[L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, flag] = initializeValues(Matrix);
[x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag);
dispFinalResults(demandedTolerance, whichIterationAreWeOn, Matrix, Vector);
2021-11-11 19:25:55 +01:00
end
2021-11-11 19:54:02 +01:00
function [L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, flag] = initializeValues(Matrix)
2021-11-11 19:25:55 +01:00
[Rows, ~] = size(Matrix);
2021-11-11 19:19:57 +01:00
[L, D, U] = decomposeMatrix(Matrix);
initial_x = ones(Rows, 1);
whichIterationAreWeOn = 0;
2021-11-11 20:12:08 +01:00
demandedTolerance = 1e-10; % as per task description
2021-11-11 19:54:02 +01:00
flag = 0;
2021-11-11 19:25:55 +01:00
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
2021-11-11 19:54:02 +01:00
function [x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag)
2021-11-11 20:12:08 +01:00
while flag ~= 1 % flag denotes whether norm(Matrix*x-Vector) <= demandedTolerance
2021-11-11 19:54:02 +01:00
[x, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector);
end
end
function [x, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector)
x = jacobiEquation(D, L, U, initial_x, Vector);
[flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector);
[initial_x, whichIterationAreWeOn] = endOfLoop(x, whichIterationAreWeOn);
end
function x = jacobiEquation(D, L, U, initial_x, Vector)
x = - D \ ( L + U ) * initial_x + D \ Vector; % As per formula
% We will be using D \ Vector and D \ ( ) instead of inverseD since
% this is faster according to matlab
end
function [flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector)
2021-11-11 20:12:08 +01:00
flag = 0;
currentError = norm(x - initial_x);
if currentError <= demandedTolerance
currentError = norm(Matrix*x-Vector);
if currentError <= demandedTolerance % if sequence as per textbook
flag = 1;
else
demandedTolerance = demandedTolerance * 2; % arbitrary value
2021-11-11 19:19:57 +01:00
end
2021-11-11 20:12:08 +01:00
end
2021-11-11 19:19:57 +01:00
end
2021-11-11 19:54:02 +01:00
function [initial_x, whichIterationAreWeOn, flag] = endOfLoop(x, whichIterationAreWeOn)
initial_x = x;
whichIterationAreWeOn = whichIterationAreWeOn + 1;
flag = 0;
end
function dispFinalResults(demandedTolerance, whichIterationAreWeOn, Matrix, Vector)
disp("Final demandedTolerance");
disp(demandedTolerance);
disp("Final Iteration: ");
disp(whichIterationAreWeOn);
disp("A\b matlab:");
disp(Matrix \ Vector);
2021-11-11 19:19:57 +01:00
end