diff --git a/ENUME/projectA/jacobiMethod.m b/ENUME/projectA/jacobiMethod.m index 14f33db3..be8f1848 100644 --- a/ENUME/projectA/jacobiMethod.m +++ b/ENUME/projectA/jacobiMethod.m @@ -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