diff --git a/ENUME/projectA/gaussSeidelMethod.asv b/ENUME/projectA/gaussSeidelMethod.asv deleted file mode 100644 index 25d0bf60..00000000 --- a/ENUME/projectA/gaussSeidelMethod.asv +++ /dev/null @@ -1,85 +0,0 @@ -function x = gaussSeidelMethod(Matrix, Vector) - [L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, flag, Rows] = initializeValues(Matrix); - [x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag, Rows); - dispFinalResults(x, demandedTolerance, whichIterationAreWeOn, Matrix, Vector); -end - -function [L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, flag, Rows] = initializeValues(Matrix) - [Rows, ~] = size(Matrix); - [L, D, U] = decomposeMatrix(Matrix); - initial_x = ones(Rows, 1); - whichIterationAreWeOn = 0; - demandedTolerance = 10e-10; % as per task description - % Minimal values I got: 3.202372833989376e-15 for both system of - % equations - original and task 2a) - flag = 0; -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, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag, Rows) - while flag ~= 1 % flag denotes whether norm(Matrix*x-Vector) <= demandedTolerance - [x, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows); - end -end - -function [x, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows) - x = jacobiEquation(D, L, U, initial_x, Vector, Rows); - [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, Rows) - W = U*initial_x - Vector; - x(1, 1) = -W(1) / D(1,1); - for i = 2 : Rows - nominator = 0; - for j = 1 : i - 1 - nominator = nominator L(i, j) * x(j); - end - nominator = nominator - W(j); - x(i, 1) = nominator / D(i, i); - end -end - -function [flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector) - flag = 0; - currentError = norm(x - initial_x); - disp(currentError); - if currentError <= demandedTolerance - currentError = norm(Matrix*x-Vector); - - if currentError <= demandedTolerance % if sequence as per textbook - flag = 1; - else - demandedTolerance = demandedTolerance * 2; % arbitrary value - end - end -end - -function [initial_x, whichIterationAreWeOn, flag] = endOfLoop(x, whichIterationAreWeOn) - initial_x = x; - whichIterationAreWeOn = whichIterationAreWeOn + 1; - flag = 0; -end - -function dispFinalResults(x, demandedTolerance, whichIterationAreWeOn, Matrix, Vector) - disp("Final demandedTolerance"); - disp(demandedTolerance); - disp("Final Iteration: "); - disp(whichIterationAreWeOn); - disp("A\b matlab:"); - disp(Matrix \ Vector); - disp("Error:"); - disp(norm(Matrix*x - Vector)); - disp("A\b error:"); - disp(norm(Matrix * (Matrix\Vector) - Vector)); -end \ No newline at end of file diff --git a/ENUME/projectA/gaussSeidelMethod.m b/ENUME/projectA/gaussSeidelMethod.m index 53f0e4b2..e9c12c4a 100644 --- a/ENUME/projectA/gaussSeidelMethod.m +++ b/ENUME/projectA/gaussSeidelMethod.m @@ -30,7 +30,7 @@ function [x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, end function [x, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows) - x = jacobiEquation(D, L, U, initial_x, Vector, Rows); + x = jacobiEquation(D, L, U, initial_x, Vector); [flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector); [initial_x, whichIterationAreWeOn] = endOfLoop(x, whichIterationAreWeOn); end diff --git a/ENUME/projectA/iterative.m b/ENUME/projectA/iterative.m new file mode 100644 index 00000000..be51e05d --- /dev/null +++ b/ENUME/projectA/iterative.m @@ -0,0 +1,114 @@ +function [x_j, x_g] = iterative(Matrix, Vector) + [L, D, U, initial_x, whichIterationAreWeOnJ, whichIterationAreWeOnG, demandedToleranceJ, demandedToleranceG, flag, Rows] = initializeValues(Matrix); + [x_j, whichIterationAreWeOnJ, demandedToleranceJ] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOnJ, demandedToleranceJ, Vector, flag); + [x_g, whichIterationAreWeOnG, demandedToleranceG] = gaussSeidelLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOnG, demandedToleranceG, Vector, flag, Rows); + dispFinalResults(x_j, x_g, demandedToleranceJ, demandedToleranceG, whichIterationAreWeOnJ, whichIterationAreWeOnG, Matrix, Vector); +end + +function [L, D, U, initial_x, whichIterationAreWeOnJ, whichIterationAreWeOnG, demandedToleranceJ, demandedToleranceG, flag, Rows] = initializeValues(Matrix) + [Rows, ~] = size(Matrix); + [L, D, U] = decomposeMatrix(Matrix); + initial_x = zeros(Rows, 1); + whichIterationAreWeOnJ = 0; + whichIterationAreWeOnG = 0; + demandedToleranceJ = 10e-10; % as per task description + demandedToleranceG = 10e-10; % as per task description + flag = 0; +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_j, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag) + while flag ~= 1 % flag denotes whether norm(Matrix*x_g-Vector) <= demandedTolerance + [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector); + end +end + +function [x_g, whichIterationAreWeOn, demandedTolerance] = gaussSeidelLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag, Rows) + while flag ~= 1 % flag denotes whether norm(Matrix*x_g-Vector) <= demandedTolerance + [x_g, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = gaussiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows); + end +end + +function [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector) + x_j = jacobiEquation(D, L, U, initial_x, Vector); + [flag, demandedTolerance] = checkError(x_j, initial_x, demandedTolerance, Matrix, Vector); + [initial_x, whichIterationAreWeOn] = endOfLoop(x_j, whichIterationAreWeOn); +end + +function [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = gaussiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows) + x_j = gaussSeidelEquation(D, L, U, initial_x, Vector, Rows); + [flag, demandedTolerance] = checkError(x_j, initial_x, demandedTolerance, Matrix, Vector); + [initial_x, whichIterationAreWeOn] = endOfLoop(x_j, 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 x_g = gaussSeidelEquation(D, L, U, initial_x, Vector, Rows) + W = U*initial_x - Vector; + x_g(1, 1) = -W(1, 1) / D(1,1); + for i = 2 : Rows + x_g(i, 1) = calculateNominator(i, L, x_g, W) / D(i, i); + end +end + +function nominator = calculateNominator(i, L, x_g, W) + nominator = 0; + for j = 1 : i - 1 + nominator = nominator + L(i, j) * x_g(j); + end + nominator = - nominator - W(j + 1, 1); +end + +function [flag, demandedTolerance] = checkError(x_g, initial_x, demandedTolerance, Matrix, Vector) + flag = 0; + currentError = norm(x_g - initial_x); + if currentError <= demandedTolerance + currentError = norm(Matrix*x_g-Vector); + + if currentError <= demandedTolerance % if sequence as per textbook + flag = 1; + else + demandedTolerance = demandedTolerance * 2; % arbitrary value + end + end +end + +function [initial_x, whichIterationAreWeOn, flag] = endOfLoop(x_g, whichIterationAreWeOn) + initial_x = x_g; + whichIterationAreWeOn = whichIterationAreWeOn + 1; + flag = 0; +end + +function dispFinalResults(x_j, x_g, demandedToleranceJ, demandedToleranceG, whichIterationAreWeOnJ, whichIterationAreWeOnG, Matrix, Vector) + disp("Final demandedTolerance for Jacobi method"); + disp(demandedToleranceJ); + disp("Final demandedTolerance for Gaussian-Seidel method:"); + disp(demandedToleranceG); + disp("Final Iteration for Jacobi method: "); + disp(whichIterationAreWeOnJ); + disp("Final Iteration for Gaussian-Seidel method: "); + disp(whichIterationAreWeOnG); + disp("Error for Jacobi method:"); + disp(norm(Matrix*x_j - Vector)); + disp("Error for Gaussian-Seidel method:"); + disp(norm(Matrix*x_g - Vector)); + disp("A\b error:"); + disp(norm(Matrix * (Matrix\Vector) - Vector)); + disp("Answer for Jacobi method: "); + disp(x_j); + disp("Answer for Gaussian-Seidel method: "); + disp(x_g); +end \ No newline at end of file diff --git a/ENUME/projectA/jacobiMethod.asv b/ENUME/projectA/jacobiMethod.asv deleted file mode 100644 index 5bf901a1..00000000 --- a/ENUME/projectA/jacobiMethod.asv +++ /dev/null @@ -1,69 +0,0 @@ -function x = jacobiMethod(Matrix, Vector) - [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)); - 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, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, currentError, demandedTolerance, Vector) - while currentError >= demandedTolerance - x = jacobiEquation(D, L, U, initial_x, Vector); - [flag, demandedTolerance] = checkError(x, initial_x, demandedTolerance, Matrix, Vector); - if flag == 1 - break - end - initial_x = x; - whichIterationAreWeOn = whichIterationAreWeOn + 1; - 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(demandedTolerance); - disp("Final Iteration: "); - disp(whichIterationAreWeOn); - disp("A\b matlab:"); - disp(Matrix \ Vector); -end \ No newline at end of file diff --git a/ENUME/projectA/projectA.aux b/ENUME/projectA/projectA.aux index 7ce60327..b5742f27 100644 --- a/ENUME/projectA/projectA.aux +++ b/ENUME/projectA/projectA.aux @@ -59,38 +59,45 @@ \@writefile{toc}{\contentsline {paragraph}{For original system of equations:}{18}{section*.14}\protected@file@percent } \@writefile{toc}{\contentsline {paragraph}{For task 2a) system of equations:}{19}{section*.15}\protected@file@percent } \@writefile{toc}{\contentsline {paragraph}{Table}{19}{section*.16}\protected@file@percent } -\@writefile{toc}{\contentsline {chapter}{\numberline {4}Problem 4 - QR method of finding eigenvalues}{20}{chapter.4}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {3.3.2}Gauss-Seidel method result}{19}{subsection.3.3.2}\protected@file@percent } +\@writefile{toc}{\contentsline {subsubsection}{Minimizing the demanded error}{21}{section*.17}\protected@file@percent } +\@writefile{toc}{\contentsline {paragraph}{For original system of equations:}{22}{section*.18}\protected@file@percent } +\@writefile{toc}{\contentsline {paragraph}{For task 2a) system of equations:}{22}{section*.19}\protected@file@percent } +\@writefile{toc}{\contentsline {paragraph}{Table}{22}{section*.20}\protected@file@percent } +\@writefile{toc}{\contentsline {chapter}{\numberline {4}Problem 4 - QR method of finding eigenvalues}{24}{chapter.4}\protected@file@percent } \@writefile{lof}{\addvspace {10\p@ }} \@writefile{lot}{\addvspace {10\p@ }} -\@writefile{toc}{\contentsline {section}{\numberline {4.1}Problem}{20}{section.4.1}\protected@file@percent } -\@writefile{toc}{\contentsline {section}{\numberline {4.2}Theoretical introduction}{20}{section.4.2}\protected@file@percent } -\@writefile{toc}{\contentsline {section}{\numberline {4.3}Solution}{20}{section.4.3}\protected@file@percent } -\@writefile{toc}{\contentsline {section}{\numberline {4.4}Discussion of the result}{20}{section.4.4}\protected@file@percent } -\@writefile{toc}{\contentsline {chapter}{\numberline {5}Code appendix}{21}{chapter.5}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {4.1}Problem}{24}{section.4.1}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {4.2}Theoretical introduction}{24}{section.4.2}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {4.3}Solution}{24}{section.4.3}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {4.4}Discussion of the result}{24}{section.4.4}\protected@file@percent } +\@writefile{toc}{\contentsline {chapter}{\numberline {5}Code appendix}{25}{chapter.5}\protected@file@percent } \@writefile{lof}{\addvspace {10\p@ }} \@writefile{lot}{\addvspace {10\p@ }} -\@writefile{toc}{\contentsline {section}{\numberline {5.1}Task 2 Code}{21}{section.5.1}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.1}Main function}{21}{subsection.5.1.1}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.2}checkIfMatrixIsSquareMatrix}{21}{subsection.5.1.2}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.3}gaussianEliminationWithPartialPivoting}{23}{subsection.5.1.3}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.4}partialPivoting}{23}{subsection.5.1.4}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.5}partialPivotingSwapOneRow}{23}{subsection.5.1.5}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.6}swapRowMatrix}{23}{subsection.5.1.6}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.7}swapValueVector}{24}{subsection.5.1.7}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.8}gaussianElimination}{24}{subsection.5.1.8}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.9}substractRows}{24}{subsection.5.1.9}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.10}backSubstitutionPhase}{25}{subsection.5.1.10}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.11}iterativeResidualCorrection}{25}{subsection.5.1.11}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.12}improveSolution}{25}{subsection.5.1.12}\protected@file@percent } -\@writefile{toc}{\contentsline {section}{\numberline {5.2}Task 3e code}{26}{section.5.2}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.1}jacobiMethod}{26}{subsection.5.2.1}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.2}initializeValues}{26}{subsection.5.2.2}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.3}decomposeMatrix}{26}{subsection.5.2.3}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.4}jacobiLoop}{27}{subsection.5.2.4}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.5}jacobiInsideLoop}{27}{subsection.5.2.5}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.6}jacobiEquation}{27}{subsection.5.2.6}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.7}checkError}{27}{subsection.5.2.7}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.8}endOfLoop}{28}{subsection.5.2.8}\protected@file@percent } -\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.9}dispFinalResults}{28}{subsection.5.2.9}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {5.1}Task 2 Code}{25}{section.5.1}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.1}Main function}{25}{subsection.5.1.1}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.2}checkIfMatrixIsSquareMatrix}{25}{subsection.5.1.2}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.3}gaussianEliminationWithPartialPivoting}{27}{subsection.5.1.3}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.4}partialPivoting}{27}{subsection.5.1.4}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.5}partialPivotingSwapOneRow}{27}{subsection.5.1.5}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.6}swapRowMatrix}{27}{subsection.5.1.6}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.7}swapValueVector}{28}{subsection.5.1.7}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.8}gaussianElimination}{28}{subsection.5.1.8}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.9}substractRows}{28}{subsection.5.1.9}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.10}backSubstitutionPhase}{29}{subsection.5.1.10}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.11}iterativeResidualCorrection}{29}{subsection.5.1.11}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.1.12}improveSolution}{29}{subsection.5.1.12}\protected@file@percent } +\@writefile{toc}{\contentsline {section}{\numberline {5.2}Task 3 code}{30}{section.5.2}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.1}initializeValues}{30}{subsection.5.2.1}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.2}decomposeMatrix}{30}{subsection.5.2.2}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.3}jacobiLoop}{31}{subsection.5.2.3}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.4}jacobiInsideLoop}{31}{subsection.5.2.4}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.5}jacobiEquation}{31}{subsection.5.2.5}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.6}gaussSeidelLoop}{32}{subsection.5.2.6}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.7}gaussiInsideLoop}{32}{subsection.5.2.7}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.8}gaussSeidelEquation}{32}{subsection.5.2.8}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.9}checkError}{32}{subsection.5.2.9}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.10}endOfLoop}{33}{subsection.5.2.10}\protected@file@percent } +\@writefile{toc}{\contentsline {subsection}{\numberline {5.2.11}dispFinalResults}{33}{subsection.5.2.11}\protected@file@percent } \bibcite{texbook}{1} -\gdef \@abspage@last{30} +\gdef \@abspage@last{36} diff --git a/ENUME/projectA/projectA.fdb_latexmk b/ENUME/projectA/projectA.fdb_latexmk index 493b0982..8673dcf7 100644 --- a/ENUME/projectA/projectA.fdb_latexmk +++ b/ENUME/projectA/projectA.fdb_latexmk @@ -1,5 +1,5 @@ # Fdb version 3 -["pdflatex"] 1636674250 "projectA.tex" "projectA.pdf" "projectA" 1636674251 +["pdflatex"] 1636680024 "projectA.tex" "projectA.pdf" "projectA" 1636680025 "/etc/texmf/web2c/texmf.cnf" 1635008344 475 c0e671620eb5563b2130f56340a5fde8 "" "/usr/share/texlive/texmf-dist/fonts/enc/dvips/base/8r.enc" 1165713224 4850 80dc9bab7f31fb78a000ccfed0e27cab "" "/usr/share/texlive/texmf-dist/fonts/map/fontname/texfonts.map" 1577235249 3524 cb3e574dea2d1052e39280babc910dc8 "" @@ -150,13 +150,13 @@ "/usr/share/texmf/web2c/texmf.cnf" 1613593815 38841 799d1dd9682a55ce442e10c99777ecc1 "" "/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map" 1635008389 5160710 ecf427ae8fa19139d8691f526e47bb9b "" "/var/lib/texmf/web2c/pdftex/pdflatex.fmt" 1635008460 2570450 6e12b1c097cbda0f70015645294afd24 "" - "projectA.aux" 1636674251 9147 1c781f2a95bf085eea7f5be3d50ec764 "pdflatex" - "projectA.out" 1636674251 3538 6fde6de672cc75e5d00cd405f1e383f3 "pdflatex" - "projectA.tex" 1636674249 32686 d604dcc57cb90d2b237ac93e70e272eb "" - "projectA.toc" 1636674251 5374 c3b887a47cd715424c93c048cb129139 "pdflatex" + "projectA.aux" 1636680025 10002 4fc10a93bf741912cf8818884a509f2f "pdflatex" + "projectA.out" 1636680025 3765 4ecb5101ef581cff275f51dba6ab5b31 "pdflatex" + "projectA.tex" 1636680023 40944 36a863bebeeed9415f2072b63916a877 "" + "projectA.toc" 1636680025 5942 df0288bcb559ef6d57b394d1a51e0fea "pdflatex" (generated) "projectA.aux" - "projectA.pdf" - "projectA.log" "projectA.out" + "projectA.pdf" "projectA.toc" + "projectA.log" diff --git a/ENUME/projectA/projectA.log b/ENUME/projectA/projectA.log index a737769b..60af2c49 100644 --- a/ENUME/projectA/projectA.log +++ b/ENUME/projectA/projectA.log @@ -1,4 +1,4 @@ -This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/Debian) (preloaded format=pdflatex 2021.10.23) 12 NOV 2021 00:44 +This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/Debian) (preloaded format=pdflatex 2021.10.23) 12 NOV 2021 02:20 entering extended mode restricted \write18 enabled. file:line:error style messages enabled. @@ -534,86 +534,114 @@ Overfull \hbox (5.74863pt too wide) in paragraph at lines 668--671 \T1/cmr/m/n/10 low as $\OT1/cmr/m/n/10 1\OML/cmm/m/it/10 :\OT1/cmr/m/n/10 776356839400250\OML/cmm/m/it/10 e \OMS/cmsy/m/n/10 ^^@ \OT1/cmr/m/n/10 15$ \T1/cmr/m/n/10 with de-manded tol-er-ance = $\OT1/cmr/m/n/10 3\OML/cmm/m/it/10 :\OT1/cmr/m/n/10 202372833989376\OML/cmm/m/it/10 e \OMS/cmsy/m/n/10 ^^@ [] -[18] [19] +[18] [19] [20] +Overfull \hbox (5.74863pt too wide) in paragraph at lines 841--844 +\T1/cmr/m/n/10 low as $\OT1/cmr/m/n/10 1\OML/cmm/m/it/10 :\OT1/cmr/m/n/10 776356839400250\OML/cmm/m/it/10 e \OMS/cmsy/m/n/10 ^^@ \OT1/cmr/m/n/10 15$ \T1/cmr/m/n/10 with de-manded tol-er-ance = $\OT1/cmr/m/n/10 3\OML/cmm/m/it/10 :\OT1/cmr/m/n/10 202372833989376\OML/cmm/m/it/10 e \OMS/cmsy/m/n/10 ^^@ + [] + +[21] [22] [23] Chapter 4. -[20 +[24 ] Chapter 5. -LaTeX Font Info: Trying to load font information for TS1+fvm on input line 759. +LaTeX Font Info: Trying to load font information for TS1+fvm on input line 931. (/usr/share/texlive/texmf-dist/tex/latex/bera/ts1fvm.fd File: ts1fvm.fd 2004/09/07 scalable font definitions for TS1/fvm. ) LaTeX Font Info: Font shape `TS1/fvm/m/n' will be -(Font) scaled to size 8.50006pt on input line 759. - [21 +(Font) scaled to size 8.50006pt on input line 931. + [25 -] [22] [23] +] [26] [27] Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 864. +(textcomp) Default family used instead on input line 1036. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 866. - [24] +(textcomp) Default family used instead on input line 1038. + [28] Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 877. +(textcomp) Default family used instead on input line 1049. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 878. +(textcomp) Default family used instead on input line 1050. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 887. +(textcomp) Default family used instead on input line 1059. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 899. +(textcomp) Default family used instead on input line 1071. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 912. +(textcomp) Default family used instead on input line 1084. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 913. - [25] +(textcomp) Default family used instead on input line 1085. + [29] Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 940. +(textcomp) Default family used instead on input line 1114. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 954. - [26] +(textcomp) Default family used instead on input line 1115. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 965. +(textcomp) Default family used instead on input line 1129. + [30] Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 988. +(textcomp) Default family used instead on input line 1140. Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 1000. +(textcomp) Default family used instead on input line 1162. + [31] Package textcomp Info: Symbol \textminus not provided by (textcomp) font family fvm in TS1 encoding. -(textcomp) Default family used instead on input line 1002. - [27] [28] [29 +(textcomp) Default family used instead on input line 1173. +Package textcomp Info: Symbol \textminus not provided by +(textcomp) font family fvm in TS1 encoding. +(textcomp) Default family used instead on input line 1195. +Package textcomp Info: Symbol \textminus not provided by +(textcomp) font family fvm in TS1 encoding. +(textcomp) Default family used instead on input line 1196. +Package textcomp Info: Symbol \textminus not provided by +(textcomp) font family fvm in TS1 encoding. +(textcomp) Default family used instead on input line 1209. + [32] +Package textcomp Info: Symbol \textminus not provided by +(textcomp) font family fvm in TS1 encoding. +(textcomp) Default family used instead on input line 1211. +Package textcomp Info: Symbol \textminus not provided by +(textcomp) font family fvm in TS1 encoding. +(textcomp) Default family used instead on input line 1246. +Package textcomp Info: Symbol \textminus not provided by +(textcomp) font family fvm in TS1 encoding. +(textcomp) Default family used instead on input line 1248. +Package textcomp Info: Symbol \textminus not provided by +(textcomp) font family fvm in TS1 encoding. +(textcomp) Default family used instead on input line 1250. + [33] [34] [35 ] (./projectA.aux) Package rerunfilecheck Info: File `projectA.out' has not changed. -(rerunfilecheck) Checksum: 6FDE6DE672CC75E5D00CD405F1E383F3;3538. +(rerunfilecheck) Checksum: 4ECB5101EF581CFF275F51DBA6AB5B31;3765. ) Here is how much of TeX's memory you used: - 12665 strings out of 479304 - 219803 string characters out of 5869778 - 945400 words of memory out of 5000000 - 29342 multiletter control sequences out of 15000+600000 + 12730 strings out of 479304 + 220761 string characters out of 5869778 + 943402 words of memory out of 5000000 + 29356 multiletter control sequences out of 15000+600000 424556 words of font info for 76 fonts, out of 8000000 for 9000 1141 hyphenation exceptions out of 8191 81i,8n,88p,407b,2253s stack positions out of 5000i,500n,10000p,200000b,80000s {/usr/share/texmf/fonts/enc/dvips/cm-super/cm-super-t1.enc}{/usr/share/texmf/fonts/enc/dvips/cm-super/cm-super-ts1.enc}{/usr/share/texlive/texmf-dist/fonts/enc/dvips/base/8r.enc} -Output written on projectA.pdf (30 pages, 315313 bytes). +Output written on projectA.pdf (36 pages, 326818 bytes). PDF statistics: - 755 PDF objects out of 1000 (max. 8388607) - 696 compressed objects within 7 object streams - 297 named destinations out of 1000 (max. 500000) - 401 words of extra memory for PDF output out of 10000 (max. 10000000) + 847 PDF objects out of 1000 (max. 8388607) + 781 compressed objects within 8 object streams + 348 named destinations out of 1000 (max. 500000) + 425 words of extra memory for PDF output out of 10000 (max. 10000000) diff --git a/ENUME/projectA/projectA.out b/ENUME/projectA/projectA.out index 0f79a6b4..09173916 100644 --- a/ENUME/projectA/projectA.out +++ b/ENUME/projectA/projectA.out @@ -19,32 +19,35 @@ \BOOKMARK [2][-]{subsection.3.2.1}{Procedure}{section.3.2}% 19 \BOOKMARK [1][-]{section.3.3}{Discussion of the result}{chapter.3}% 20 \BOOKMARK [2][-]{subsection.3.3.1}{Jacobi method result}{section.3.3}% 21 -\BOOKMARK [0][-]{chapter.4}{Problem 4 - QR method of finding eigenvalues}{}% 22 -\BOOKMARK [1][-]{section.4.1}{Problem}{chapter.4}% 23 -\BOOKMARK [1][-]{section.4.2}{Theoretical introduction}{chapter.4}% 24 -\BOOKMARK [1][-]{section.4.3}{Solution}{chapter.4}% 25 -\BOOKMARK [1][-]{section.4.4}{Discussion of the result}{chapter.4}% 26 -\BOOKMARK [0][-]{chapter.5}{Code appendix}{}% 27 -\BOOKMARK [1][-]{section.5.1}{Task 2 Code}{chapter.5}% 28 -\BOOKMARK [2][-]{subsection.5.1.1}{Main function}{section.5.1}% 29 -\BOOKMARK [2][-]{subsection.5.1.2}{checkIfMatrixIsSquareMatrix}{section.5.1}% 30 -\BOOKMARK [2][-]{subsection.5.1.3}{gaussianEliminationWithPartialPivoting}{section.5.1}% 31 -\BOOKMARK [2][-]{subsection.5.1.4}{partialPivoting}{section.5.1}% 32 -\BOOKMARK [2][-]{subsection.5.1.5}{partialPivotingSwapOneRow}{section.5.1}% 33 -\BOOKMARK [2][-]{subsection.5.1.6}{swapRowMatrix}{section.5.1}% 34 -\BOOKMARK [2][-]{subsection.5.1.7}{swapValueVector}{section.5.1}% 35 -\BOOKMARK [2][-]{subsection.5.1.8}{gaussianElimination}{section.5.1}% 36 -\BOOKMARK [2][-]{subsection.5.1.9}{substractRows}{section.5.1}% 37 -\BOOKMARK [2][-]{subsection.5.1.10}{backSubstitutionPhase}{section.5.1}% 38 -\BOOKMARK [2][-]{subsection.5.1.11}{iterativeResidualCorrection}{section.5.1}% 39 -\BOOKMARK [2][-]{subsection.5.1.12}{improveSolution}{section.5.1}% 40 -\BOOKMARK [1][-]{section.5.2}{Task 3e code}{chapter.5}% 41 -\BOOKMARK [2][-]{subsection.5.2.1}{jacobiMethod}{section.5.2}% 42 -\BOOKMARK [2][-]{subsection.5.2.2}{initializeValues}{section.5.2}% 43 -\BOOKMARK [2][-]{subsection.5.2.3}{decomposeMatrix}{section.5.2}% 44 -\BOOKMARK [2][-]{subsection.5.2.4}{jacobiLoop}{section.5.2}% 45 -\BOOKMARK [2][-]{subsection.5.2.5}{jacobiInsideLoop}{section.5.2}% 46 -\BOOKMARK [2][-]{subsection.5.2.6}{jacobiEquation}{section.5.2}% 47 -\BOOKMARK [2][-]{subsection.5.2.7}{checkError}{section.5.2}% 48 -\BOOKMARK [2][-]{subsection.5.2.8}{endOfLoop}{section.5.2}% 49 -\BOOKMARK [2][-]{subsection.5.2.9}{dispFinalResults}{section.5.2}% 50 +\BOOKMARK [2][-]{subsection.3.3.2}{Gauss-Seidel method result}{section.3.3}% 22 +\BOOKMARK [0][-]{chapter.4}{Problem 4 - QR method of finding eigenvalues}{}% 23 +\BOOKMARK [1][-]{section.4.1}{Problem}{chapter.4}% 24 +\BOOKMARK [1][-]{section.4.2}{Theoretical introduction}{chapter.4}% 25 +\BOOKMARK [1][-]{section.4.3}{Solution}{chapter.4}% 26 +\BOOKMARK [1][-]{section.4.4}{Discussion of the result}{chapter.4}% 27 +\BOOKMARK [0][-]{chapter.5}{Code appendix}{}% 28 +\BOOKMARK [1][-]{section.5.1}{Task 2 Code}{chapter.5}% 29 +\BOOKMARK [2][-]{subsection.5.1.1}{Main function}{section.5.1}% 30 +\BOOKMARK [2][-]{subsection.5.1.2}{checkIfMatrixIsSquareMatrix}{section.5.1}% 31 +\BOOKMARK [2][-]{subsection.5.1.3}{gaussianEliminationWithPartialPivoting}{section.5.1}% 32 +\BOOKMARK [2][-]{subsection.5.1.4}{partialPivoting}{section.5.1}% 33 +\BOOKMARK [2][-]{subsection.5.1.5}{partialPivotingSwapOneRow}{section.5.1}% 34 +\BOOKMARK [2][-]{subsection.5.1.6}{swapRowMatrix}{section.5.1}% 35 +\BOOKMARK [2][-]{subsection.5.1.7}{swapValueVector}{section.5.1}% 36 +\BOOKMARK [2][-]{subsection.5.1.8}{gaussianElimination}{section.5.1}% 37 +\BOOKMARK [2][-]{subsection.5.1.9}{substractRows}{section.5.1}% 38 +\BOOKMARK [2][-]{subsection.5.1.10}{backSubstitutionPhase}{section.5.1}% 39 +\BOOKMARK [2][-]{subsection.5.1.11}{iterativeResidualCorrection}{section.5.1}% 40 +\BOOKMARK [2][-]{subsection.5.1.12}{improveSolution}{section.5.1}% 41 +\BOOKMARK [1][-]{section.5.2}{Task 3 code}{chapter.5}% 42 +\BOOKMARK [2][-]{subsection.5.2.1}{initializeValues}{section.5.2}% 43 +\BOOKMARK [2][-]{subsection.5.2.2}{decomposeMatrix}{section.5.2}% 44 +\BOOKMARK [2][-]{subsection.5.2.3}{jacobiLoop}{section.5.2}% 45 +\BOOKMARK [2][-]{subsection.5.2.4}{jacobiInsideLoop}{section.5.2}% 46 +\BOOKMARK [2][-]{subsection.5.2.5}{jacobiEquation}{section.5.2}% 47 +\BOOKMARK [2][-]{subsection.5.2.6}{gaussSeidelLoop}{section.5.2}% 48 +\BOOKMARK [2][-]{subsection.5.2.7}{gaussiInsideLoop}{section.5.2}% 49 +\BOOKMARK [2][-]{subsection.5.2.8}{gaussSeidelEquation}{section.5.2}% 50 +\BOOKMARK [2][-]{subsection.5.2.9}{checkError}{section.5.2}% 51 +\BOOKMARK [2][-]{subsection.5.2.10}{endOfLoop}{section.5.2}% 52 +\BOOKMARK [2][-]{subsection.5.2.11}{dispFinalResults}{section.5.2}% 53 diff --git a/ENUME/projectA/projectA.pdf b/ENUME/projectA/projectA.pdf index 800b2458..b34f0c4a 100644 Binary files a/ENUME/projectA/projectA.pdf and b/ENUME/projectA/projectA.pdf differ diff --git a/ENUME/projectA/projectA.synctex.gz b/ENUME/projectA/projectA.synctex.gz index 962ab33a..e38eeca3 100644 Binary files a/ENUME/projectA/projectA.synctex.gz and b/ENUME/projectA/projectA.synctex.gz differ diff --git a/ENUME/projectA/projectA.tex b/ENUME/projectA/projectA.tex index e6bbce9b..03e0a172 100755 --- a/ENUME/projectA/projectA.tex +++ b/ENUME/projectA/projectA.tex @@ -734,6 +734,178 @@ We managed to achieve slightly better (as in, the error was smaller) results tha \end{tabular}} \end{center} +\subsection{Gauss-Seidel method result} +For system of equations We got in this task We got following results: +\\ +Without the change in demanded tolerance: +\[ x = \left( \begin{array}{cc} + -0.076776098668341 \\ + 2.105784262642568 \\ + 0.395344797635474 \\ + 0.397776619764909 +\end{array} \right) +\] +Error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 1.154375287358407e-10 \] +We managed to do this in \textbf{38} iterations of our loop, and the demanded tolerance did not change. (This required small change in code where We ommited the part of code responsible for changing demandedTolerance if $ \| \mathbf{A}x-b \| > \delta_2) $ ) + +With the change in demanded tolerance: +\[ x = \left( \begin{array}{cc} + -0.076776098668341 \\ + 2.105784262642568 \\ + 0.395344797635474 \\ + 0.397776619764909 +\end{array} \right) +\] +Error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 5.770361548895147e-11 \] +We got this result in \textbf{37} iterations and demanded tolerance was equal to $2*10^{-10}$ + +Compared to matlab function +\[ x_{matlab} = \left( \begin{array}{cc} + -0.076776098662498 \\ + 2.105784262636790 \\ + 0.395344797637659 \\ + 0.397776619767240 \\ +\end{array} \right) +\] +Matlab error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 4.070144838902081e-15 \] + +For data from task 2a We got: \\ +Without change in demanded tolerance: +\[ x_a = \left( \begin{array}{cc} +-0.930024655108186 \\ +-1.223407298660663 \\ +-1.273530574212508 \\ +-1.230517757317628 \\ +-1.151356031082747 \\ +-1.056883669273682 \\ +-0.952628310081466 \\ +-0.834334594312996 \\ +-0.683708806198363 \\ +-0.450125157620744 \\ +\end{array} \right) +\] +Error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 6.955194519943778e-11 \] +We managed to do this in \textbf{59} iterations of our loop, and the demanded tolerance did not change. + +With change in demanded tolerance: +\[ x_a = \left( \begin{array}{cc} +-0.930024655104470 \\ +-1.223407298653515 \\ +-1.273530574202540 \\ +-1.230517757305602 \\ +-1.151356031069692 \\ +-1.056883669260597 \\ +-0.952628310069469 \\ +-0.834334594303006 \\ +-0.683708806191233 \\ +-0.450125157617020 \\ +\end{array} \right) +\] +Error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 1.699812218689508e-10 \] +We managed to do this in \textbf{57} iterations of our loop, and the demanded tolerance changed to $4*10^{-10}$ + +Compared to matlab $ A \ b $ function +\[ x_{matlab} = \left( \begin{array}{cc} + -0.930024655110760 \\ + -1.223407298665612 \\ + -1.273530574219411 \\ + -1.230517757325956 \\ + -1.151356031091789 \\ + -1.056883669282743 \\ + -0.952628310089775 \\ + -0.834334594319914 \\ + -0.683708806203301 \\ + -0.450125157623323 \\ +\end{array} \right) +\] +Matlab error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 3.662053438817790e-15 \] + +For Matrix and Vector from task 2b) error of +\[ \| x^{(i+1)} - x^{(i)} \| \] +grew to infinity, therefore We could never achieve demanded tolerance, therefore the program executed infinite loop. + +\subsubsection{Minimizing the demanded error} +We tried to minimize the demanded error using this steps: +\begin{enumerate} +\item We copied error from matlab function and pasted it into demanded tolerance. +\item If We did not get infinite loop We copied the newly acquired error and pasted it into demanded tolerance. +\item If We got inifinite loop We used the previous error as "minimal" demanded error. +\end{enumerate} +\paragraph{For original system of equations:} +We managed to get results with error as low as $1.776356839400250e-15$ with demanded tolerance = $3.202372833989376e-15$ for lower values program went into infinite loop. +Results for demanded tolerance = $3.202372833989376e-15$ +For given matrix: +\[ x = \left( \begin{array}{cc} + -0.076776098662498 \\ + 2.105784262636790 \\ + 0.395344797637659 \\ + 0.397776619767240 +\end{array} \right) +\] +Error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 3.108624468950438e-15 \] +We got this result in \textbf{53} iterations and demanded tolerance did not change. + +\paragraph{For task 2a) system of equations:} +We managed to get results with error as low as +\[ 3.108624468950438e-15 \] with demanded tolerance: +\[ 3.202372833989376e-15 \] +for lower values program went into infinite loop. + +For demanded tolerance = $3.202372833989376e-15$: +Results for 2a) system of equation + +\[ x_a = \left( \begin{array}{cc} +-0.930024655110760 \\ +-1.223407298665613 \\ +-1.273530574219411 \\ +-1.230517757325955 \\ +-1.151356031091788 \\ +-1.056883669282743 \\ +-0.952628310089775 \\ +-0.834334594319914 \\ +-0.683708806203301 \\ +-0.450125157623323 +\end{array} \right) +\] +Error: +\[ r = \| \mathbf{A}\mathbf{x} - \mathbf{b}\| = 3.108624468950438e-15 \] +We managed to do this in \textbf{84} iterations of our loop, and the demanded tolerance did not change. +We managed to achieve slightly better (as in, the error was smaller) results than Matlab custom function. + +\paragraph{Table} + +\begin{center} +\resizebox{\textwidth}{!}{ +\begin{tabular}{||c c c c c c||} + \hline + system of equations & method & demanded tolerance & final demanded tolerance & error & iterations \\ + \hline + task 3 system & Jacobi method & 10e-10 & 10e-10 & 1.154375287358407e-10 & 38 \\ + \hline + task 3 system & Jacobi method & 10e-10 & 20e-10 & 5.770361548895147e-11 & 37 \\ + \hline + task 3 system & Jacobi method & 3.202372833989376e-15 & 3.202372833989376e-15 & 3.108624468950438e-15 & 53 \\ + \hline + task 3 system & Matlab function & ? & ? & 4.070144838902081e-15 & ? \\ + \hline + task 2a) system & Jacobi method & 10e-10 & 10e-10 & 6.955194519943778e-11 & 59 \\ + \hline + task 2a) system & Jacobi method & 10e-10 & 40e-10 & 1.699812218689508e-10 & 57 \\ + \hline + task 2a) system & Jacob method & 3.202372833989376e-15 & 3.202372833989376e-15 & 3.108624468950438e-15 & 84 \\ + \hline + task 2a) system & Matlab function & ? & ? & 3.662053438817790e-15 & ? \\ + \hline + +\end{tabular}} +\end{center} \chapter{Problem 4 - QR method of finding eigenvalues} @@ -917,27 +1089,30 @@ end % end function \end{lstlisting} \end{simplechar} -\section{Task 3e code} -\subsection{jacobiMethod} +\section{Task 3 code} \begin{simplechar} \begin{lstlisting} -function x = jacobiMethod(Matrix, Vector) - [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); +function [x_j, x_g] = iterative(Matrix, Vector) + [L, D, U, initial_x, whichIterationAreWeOnJ, whichIterationAreWeOnG, demandedToleranceJ, demandedToleranceG, flag, Rows] = initializeValues(Matrix); + [x_j, whichIterationAreWeOnJ, demandedToleranceJ] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOnJ, demandedToleranceJ, Vector, flag); + [x_g, whichIterationAreWeOnG, demandedToleranceG] = gaussSeidelLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOnG, demandedToleranceG, Vector, flag, Rows); + dispFinalResults(x_j, x_g, demandedToleranceJ, demandedToleranceG, whichIterationAreWeOnJ, whichIterationAreWeOnG, Matrix, Vector); end + \end{lstlisting} \end{simplechar} \subsection{initializeValues} \begin{simplechar} \begin{lstlisting} -function [L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, flag] = initializeValues(Matrix) +function [L, D, U, initial_x, whichIterationAreWeOnJ, whichIterationAreWeOnG, demandedToleranceJ, demandedToleranceG, flag, Rows] = initializeValues(Matrix) [Rows, ~] = size(Matrix); [L, D, U] = decomposeMatrix(Matrix); - initial_x = ones(Rows, 1); - whichIterationAreWeOn = 0; - demandedTolerance = 1e-10; % as per task description + initial_x = zeros(Rows, 1); + whichIterationAreWeOnJ = 0; + whichIterationAreWeOnG = 0; + demandedToleranceJ = 10e-10; % as per task description + demandedToleranceG = 10e-10; % as per task description flag = 0; end \end{lstlisting} @@ -949,10 +1124,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 + % 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 + % where the second variable denotes on which diagonal of matrix should we % start end \end{lstlisting} @@ -961,22 +1136,21 @@ end \subsection{jacobiLoop} \begin{simplechar} \begin{lstlisting} -function [x, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag) - while flag ~= 1 % flag denotes whether norm(Matrix*x-Vector) <= demandedTolerance - [x, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector); - end +function [x_j, whichIterationAreWeOn, demandedTolerance] = jacobiLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag) + while flag ~= 1 % flag denotes whether norm(Matrix*x_g-Vector) <= demandedTolerance + [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector); + end end - \end{lstlisting} \end{simplechar} \subsection{jacobiInsideLoop} \begin{simplechar} \begin{lstlisting} -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); +function [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = jacobiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector) + x_j = jacobiEquation(D, L, U, initial_x, Vector); + [flag, demandedTolerance] = checkError(x_j, initial_x, demandedTolerance, Matrix, Vector); + [initial_x, whichIterationAreWeOn] = endOfLoop(x_j, whichIterationAreWeOn); end \end{lstlisting} \end{simplechar} @@ -992,6 +1166,41 @@ end \end{lstlisting} \end{simplechar} +\subsection{gaussSeidelLoop} +\begin{simplechar} +\begin{lstlisting} +function [x_g, whichIterationAreWeOn, demandedTolerance] = gaussSeidelLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, flag, Rows) + while flag ~= 1 % flag denotes whether norm(Matrix*x_g-Vector) <= demandedTolerance + [x_g, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = gaussiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows); + end +end +\end{lstlisting} +\end{simplechar} + +\subsection{gaussiInsideLoop} +\begin{simplechar} +\begin{lstlisting} +function [x_j, whichIterationAreWeOn, demandedTolerance, flag, initial_x] = gaussiInsideLoop(Matrix, L, D, U, initial_x, whichIterationAreWeOn, demandedTolerance, Vector, Rows) + x_j = gaussSeidelEquation(D, L, U, initial_x, Vector, Rows); + [flag, demandedTolerance] = checkError(x_j, initial_x, demandedTolerance, Matrix, Vector); + [initial_x, whichIterationAreWeOn] = endOfLoop(x_j, whichIterationAreWeOn); +end +\end{lstlisting} +\end{simplechar} + +\subsection{gaussSeidelEquation} +\begin{simplechar} +\begin{lstlisting} +function x_g = gaussSeidelEquation(D, L, U, initial_x, Vector, Rows) + W = U*initial_x - Vector; + x_g(1, 1) = -W(1, 1) / D(1,1); + for i = 2 : Rows + x_g(i, 1) = calculateNominator(i, L, x_g, W) / D(i, i); + end +end +\end{lstlisting} +\end{simplechar} + \subsection{checkError} \begin{simplechar} \begin{lstlisting} @@ -1024,13 +1233,25 @@ end \subsection{dispFinalResults} \begin{simplechar} \begin{lstlisting} -function dispFinalResults(demandedTolerance, whichIterationAreWeOn, Matrix, Vector) - disp("Final demandedTolerance"); - disp(demandedTolerance); - disp("Final Iteration: "); - disp(whichIterationAreWeOn); - disp("A\b matlab:"); - disp(Matrix \ Vector); +function dispFinalResults(x_j, x_g, demandedToleranceJ, demandedToleranceG, whichIterationAreWeOnJ, whichIterationAreWeOnG, Matrix, Vector) + disp("Final demandedTolerance for Jacobi method"); + disp(demandedToleranceJ); + disp("Final demandedTolerance for Gaussian-Seidel method:"); + disp(demandedToleranceG); + disp("Final Iteration for Jacobi method: "); + disp(whichIterationAreWeOnJ); + disp("Final Iteration for Gaussian-Seidel method: "); + disp(whichIterationAreWeOnG); + disp("Error for Jacobi method:"); + disp(norm(Matrix*x_j - Vector)); + disp("Error for Gaussian-Seidel method:"); + disp(norm(Matrix*x_g - Vector)); + disp("A\b error:"); + disp(norm(Matrix * (Matrix\Vector) - Vector)); + disp("Answer for Jacobi method: "); + disp(x_j); + disp("Answer for Gaussian-Seidel method: "); + disp(x_g); end \end{lstlisting} \end{simplechar} diff --git a/ENUME/projectA/projectA.toc b/ENUME/projectA/projectA.toc index 0dc730b2..0e66c654 100644 --- a/ENUME/projectA/projectA.toc +++ b/ENUME/projectA/projectA.toc @@ -35,32 +35,39 @@ \contentsline {paragraph}{For original system of equations:}{18}{section*.14}% \contentsline {paragraph}{For task 2a) system of equations:}{19}{section*.15}% \contentsline {paragraph}{Table}{19}{section*.16}% -\contentsline {chapter}{\numberline {4}Problem 4 - QR method of finding eigenvalues}{20}{chapter.4}% -\contentsline {section}{\numberline {4.1}Problem}{20}{section.4.1}% -\contentsline {section}{\numberline {4.2}Theoretical introduction}{20}{section.4.2}% -\contentsline {section}{\numberline {4.3}Solution}{20}{section.4.3}% -\contentsline {section}{\numberline {4.4}Discussion of the result}{20}{section.4.4}% -\contentsline {chapter}{\numberline {5}Code appendix}{21}{chapter.5}% -\contentsline {section}{\numberline {5.1}Task 2 Code}{21}{section.5.1}% -\contentsline {subsection}{\numberline {5.1.1}Main function}{21}{subsection.5.1.1}% -\contentsline {subsection}{\numberline {5.1.2}checkIfMatrixIsSquareMatrix}{21}{subsection.5.1.2}% -\contentsline {subsection}{\numberline {5.1.3}gaussianEliminationWithPartialPivoting}{23}{subsection.5.1.3}% -\contentsline {subsection}{\numberline {5.1.4}partialPivoting}{23}{subsection.5.1.4}% -\contentsline {subsection}{\numberline {5.1.5}partialPivotingSwapOneRow}{23}{subsection.5.1.5}% -\contentsline {subsection}{\numberline {5.1.6}swapRowMatrix}{23}{subsection.5.1.6}% -\contentsline {subsection}{\numberline {5.1.7}swapValueVector}{24}{subsection.5.1.7}% -\contentsline {subsection}{\numberline {5.1.8}gaussianElimination}{24}{subsection.5.1.8}% -\contentsline {subsection}{\numberline {5.1.9}substractRows}{24}{subsection.5.1.9}% -\contentsline {subsection}{\numberline {5.1.10}backSubstitutionPhase}{25}{subsection.5.1.10}% -\contentsline {subsection}{\numberline {5.1.11}iterativeResidualCorrection}{25}{subsection.5.1.11}% -\contentsline {subsection}{\numberline {5.1.12}improveSolution}{25}{subsection.5.1.12}% -\contentsline {section}{\numberline {5.2}Task 3e code}{26}{section.5.2}% -\contentsline {subsection}{\numberline {5.2.1}jacobiMethod}{26}{subsection.5.2.1}% -\contentsline {subsection}{\numberline {5.2.2}initializeValues}{26}{subsection.5.2.2}% -\contentsline {subsection}{\numberline {5.2.3}decomposeMatrix}{26}{subsection.5.2.3}% -\contentsline {subsection}{\numberline {5.2.4}jacobiLoop}{27}{subsection.5.2.4}% -\contentsline {subsection}{\numberline {5.2.5}jacobiInsideLoop}{27}{subsection.5.2.5}% -\contentsline {subsection}{\numberline {5.2.6}jacobiEquation}{27}{subsection.5.2.6}% -\contentsline {subsection}{\numberline {5.2.7}checkError}{27}{subsection.5.2.7}% -\contentsline {subsection}{\numberline {5.2.8}endOfLoop}{28}{subsection.5.2.8}% -\contentsline {subsection}{\numberline {5.2.9}dispFinalResults}{28}{subsection.5.2.9}% +\contentsline {subsection}{\numberline {3.3.2}Gauss-Seidel method result}{19}{subsection.3.3.2}% +\contentsline {subsubsection}{Minimizing the demanded error}{21}{section*.17}% +\contentsline {paragraph}{For original system of equations:}{22}{section*.18}% +\contentsline {paragraph}{For task 2a) system of equations:}{22}{section*.19}% +\contentsline {paragraph}{Table}{22}{section*.20}% +\contentsline {chapter}{\numberline {4}Problem 4 - QR method of finding eigenvalues}{24}{chapter.4}% +\contentsline {section}{\numberline {4.1}Problem}{24}{section.4.1}% +\contentsline {section}{\numberline {4.2}Theoretical introduction}{24}{section.4.2}% +\contentsline {section}{\numberline {4.3}Solution}{24}{section.4.3}% +\contentsline {section}{\numberline {4.4}Discussion of the result}{24}{section.4.4}% +\contentsline {chapter}{\numberline {5}Code appendix}{25}{chapter.5}% +\contentsline {section}{\numberline {5.1}Task 2 Code}{25}{section.5.1}% +\contentsline {subsection}{\numberline {5.1.1}Main function}{25}{subsection.5.1.1}% +\contentsline {subsection}{\numberline {5.1.2}checkIfMatrixIsSquareMatrix}{25}{subsection.5.1.2}% +\contentsline {subsection}{\numberline {5.1.3}gaussianEliminationWithPartialPivoting}{27}{subsection.5.1.3}% +\contentsline {subsection}{\numberline {5.1.4}partialPivoting}{27}{subsection.5.1.4}% +\contentsline {subsection}{\numberline {5.1.5}partialPivotingSwapOneRow}{27}{subsection.5.1.5}% +\contentsline {subsection}{\numberline {5.1.6}swapRowMatrix}{27}{subsection.5.1.6}% +\contentsline {subsection}{\numberline {5.1.7}swapValueVector}{28}{subsection.5.1.7}% +\contentsline {subsection}{\numberline {5.1.8}gaussianElimination}{28}{subsection.5.1.8}% +\contentsline {subsection}{\numberline {5.1.9}substractRows}{28}{subsection.5.1.9}% +\contentsline {subsection}{\numberline {5.1.10}backSubstitutionPhase}{29}{subsection.5.1.10}% +\contentsline {subsection}{\numberline {5.1.11}iterativeResidualCorrection}{29}{subsection.5.1.11}% +\contentsline {subsection}{\numberline {5.1.12}improveSolution}{29}{subsection.5.1.12}% +\contentsline {section}{\numberline {5.2}Task 3 code}{30}{section.5.2}% +\contentsline {subsection}{\numberline {5.2.1}initializeValues}{30}{subsection.5.2.1}% +\contentsline {subsection}{\numberline {5.2.2}decomposeMatrix}{30}{subsection.5.2.2}% +\contentsline {subsection}{\numberline {5.2.3}jacobiLoop}{31}{subsection.5.2.3}% +\contentsline {subsection}{\numberline {5.2.4}jacobiInsideLoop}{31}{subsection.5.2.4}% +\contentsline {subsection}{\numberline {5.2.5}jacobiEquation}{31}{subsection.5.2.5}% +\contentsline {subsection}{\numberline {5.2.6}gaussSeidelLoop}{32}{subsection.5.2.6}% +\contentsline {subsection}{\numberline {5.2.7}gaussiInsideLoop}{32}{subsection.5.2.7}% +\contentsline {subsection}{\numberline {5.2.8}gaussSeidelEquation}{32}{subsection.5.2.8}% +\contentsline {subsection}{\numberline {5.2.9}checkError}{32}{subsection.5.2.9}% +\contentsline {subsection}{\numberline {5.2.10}endOfLoop}{33}{subsection.5.2.10}% +\contentsline {subsection}{\numberline {5.2.11}dispFinalResults}{33}{subsection.5.2.11}%