Task 2 more functions

This commit is contained in:
PolishPigeon 2021-11-11 19:54:02 +01:00
parent d5089c4a9e
commit a7d9a9da18
2 changed files with 93 additions and 51 deletions

View File

@ -1,43 +1,69 @@
function x = jacobiMethod(Matrix, Vector) function x = jacobiMethod(Matrix, Vector)
[Rows,~] = size(Matrix); [L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance] = initializeValues(Matrix);
[x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance, Vector);
dispFinalResults(demandedTolerance, whichIterationAreWeOn, 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)); D = diag(diag(Matrix));
inverseD = inv(D);
U = triu(Matrix, 1); % Generates upper triangular part of matrix U = triu(Matrix, 1); % Generates upper triangular part of matrix
% where the second variable denotes on which diagonal of matrix should we % where the second variable denotes on which diagonal of matrix should we
% start % start
L = tril(Matrix, -1); % Generates lower triangular part of matrix L = tril(Matrix, -1); % Generates lower triangular part of matrix
% where the second variable denotes on which diagonal of matrix should we % where the second variable denotes on which diagonal of matrix should we
% start % start
initial_x = ones(Rows, 1); end
initial_x = - inverseD * ( ( L + U ) * initial_x) + inverseD * initial_x; % As per formula
% We will be using D \ initial_x and D \ () since it is faster and more function [x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance, Vector)
% accurate according to matlab
whichIterationAreWeOn = 0;
currentError = inf; % We set it to inf so that it the algorithm will always start
% (See condition below)
demandedTolerance = 1e-10;
while currentError >= demandedTolerance while currentError >= demandedTolerance
x = jacobiEquation(D, L, U, initial_x, Vector);
x = - inverseD * ( ( L + U ) * initial_x) + inverseD * initial_x; % As per formula [flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector);
if flag == 1
currentError = norm(x - initial_x); break
disp(currentError);
if currentError <= demandedTolerance
currentError = norm(Matrix*x-Vector);
disp(currentError);
if currentError <= demandedTolerance
break;
else
demandedTolerance = demandedTolerance * 2;
end
end end
initial_x = x; initial_x = x;
whichIterationAreWeOn = whichIterationAreWeOn + 1; whichIterationAreWeOn = whichIterationAreWeOn + 1;
end end
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)
flag = 0;
currentError = norm(x - initial_x);
if currentError <= demandedTolerance
currentError = norm(Matrix*x-Vector);
if currentError <= demandedTolerance
flag = 1;
else
demandedTolerance = demandedTolerance * 2;
end
end
end
function [initial_x, ]
function dispFinalResults(demandedTolerance, whichIterationAreWeOn, Matrix, Vector)
disp("Final demandedTolerance"); disp("Final demandedTolerance");
disp(demandedTolerance); disp(demandedTolerance);
disp("Final Iteration: "); disp("Final Iteration: ");
disp(whichIterationAreWeOn); disp(whichIterationAreWeOn);
disp("A\b matlab:"); disp("A\b matlab:");
disp(Matrix / Vector); disp(Matrix \ Vector);
end end

View File

@ -1,22 +1,16 @@
function x = jacobiMethod(Matrix, Vector) function x = jacobiMethod(Matrix, Vector)
[L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance] = initializeValues(Matrix) [L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, flag] = initializeValues(Matrix);
x = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance, Vector); [x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag);
disp("Final demandedTolerance"); dispFinalResults(demandedTolerance, whichIterationAreWeOn, Matrix, Vector);
disp(demandedTolerance);
disp("Final Iteration: ");
disp(whichIterationAreWeOn);
disp("A\b matlab:");
disp(Matrix \ Vector);
end end
function [L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance] = initializeValues(Matrix) function [L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, flag] = 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);
whichIterationAreWeOn = 0; whichIterationAreWeOn = 0;
currentError = inf; % We set it to inf so that the algorithm will always start
% (See condition below)
demandedTolerance = 1e-10; demandedTolerance = 1e-10;
flag = 0;
end end
function [L, D, U] = decomposeMatrix(Matrix) function [L, D, U] = decomposeMatrix(Matrix)
@ -29,28 +23,50 @@ function [L, D, U] = decomposeMatrix(Matrix)
% start % start
end end
function x = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance, Vector) function [x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag)
while currentError >= demandedTolerance while flag ~= 1
x = jacobiEquation(D, L, U, initial_x, Vector); [x, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector);
currentError = norm(x - initial_x);
%disp(currentError);
if currentError <= demandedTolerance
currentError = norm(Matrix*x-Vector);
%disp(currentError);
if currentError <= demandedTolerance
break;
else
demandedTolerance = demandedTolerance * 2;
end
end
initial_x = x;
whichIterationAreWeOn = whichIterationAreWeOn + 1;
end end
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) function x = jacobiEquation(D, L, U, initial_x, Vector)
x = - D \ ( L + U ) * initial_x + D \ Vector; % As per formula x = - D \ ( L + U ) * initial_x + D \ Vector; % As per formula
% We will be using D \ Vector and D \ ( ) instead of inverseD since % We will be using D \ Vector and D \ ( ) instead of inverseD since
% this is faster according to matlab % this is faster according to matlab
end
function [flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector)
flag = 0;
currentError = norm(x - initial_x);
if currentError <= demandedTolerance
currentError = norm(Matrix*x-Vector);
if currentError <= demandedTolerance
flag = 1;
else
demandedTolerance = demandedTolerance * 2;
end
end
end
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);
end end