diff --git a/ENUME/projectA/indicatedMethod.asv b/ENUME/projectA/indicatedMethod.asv new file mode 100644 index 00000000..eac24359 --- /dev/null +++ b/ENUME/projectA/indicatedMethod.asv @@ -0,0 +1,117 @@ +function x = indicatedMethod(Matrix, Vector) % Name of the method as in the textbook +% x stands for obtained result + checkIfMatrixIsSquareMatrix(Matrix); + [Matrix, Vector, x] = solveSystem(Matrix, Vector); + originalSolution = x; + errorBeforeResidualCorrection = norm(Matrix*x - Vector); + x = iterativeResidualCorrection(Matrix, x, Vector); % Improve on the solution + disp("errorBeforeResidualCorrection") + disp(errorBeforeResidualCorrection); + disp("errorAfterResidualCorrection") + disp(norm(Matrix*x - Vector)); + disp("Solution before residual correction:") + disp(originalSolution); + disp("Solution after residual correction:") + disp(x); + disp("A\b solution:") + disp(Matrix\Vector); + disp("A\b error:") + disp(Matrix\Vector - ); + +end % end function + +function [Matrix, Vector, x] = solveSystem(Matrix, Vector) + [~,Columns] = size(Matrix); % We need to know how big the matrix is in next steps + % notice the '~', since we assume we use square matrix, we do not need + % to have another variable for number of rows since it is the same as + % number of columns + [Matrix, Vector] = gaussianEliminationWithPartialPivoting(Columns, Matrix, Vector); + % Change matrix to upper triangular matrix + [Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector); + % Get the solution +end % end function + +function checkIfMatrixIsSquareMatrix(Matrix) + [Rows,Columns] = size(Matrix); + if Rows ~= Columns + error ('Matrix is not square matrix!'); + end % end if +end % end function + +function [Matrix, Vector] = gaussianEliminationWithPartialPivoting(Columns, Matrix, Vector) + for j = 1 : Columns + centralElement = max(Matrix(j:Columns,j)); + % we stay in the same row (j) but we change columns, as in the + % textbook + [Matrix, Vector] = partialPivoting(Matrix, Vector, j, centralElement, Columns); + % ensures that a_kk != 0 and reduces errors + [Matrix, Vector] = gaussianElimination(j, Columns, Matrix, Vector); + % change matrix into upper triangular matrix + end % end for +end % end function + +function [Matrix, Vector] = partialPivoting(Matrix, Vector, j, centralElement, Columns) + for k = j : Columns + partialPivotingSwapOneRow(Matrix, Vector, j, k, centralElement); + end % end for +end % end function + +function [Matrix, Vector] = partialPivotingSwapOneRow(Matrix, Vector, j, k, centralElement) + if Matrix(k,j) == centralElement + swapRowMatrix(Matrix, j, k); % swap jth row with kth row + swapValueVector(Vector, j, k); % swap jth value with kth value + end % end if +end % end function + +function Matrix = swapRowMatrix(Matrix, j, k) + temp = Matrix(j , :); % ' : ' denote "all elements in jth row" + Matrix(j , :) = Matrix(k, :); + Matrix(k, :) = temp; % temp equal to previous value of jth row +end + +function Vector = swapValueVector(Vector, j, k) + temp = Vector(j); + Vector(j) = Vector(k); + Vector(k) = temp; % temp equal to previous value of k element of vector +end % end function + +function [Matrix, Vector] = gaussianElimination(j, Columns, Matrix, Vector) + for i = j + 1 : Columns + rowMultiplier = Matrix(i,j) / Matrix(j,j); + [Matrix, Vector] = substractRows(Matrix, Vector, i, rowMultiplier, j, Columns); + end % end for +end % end function + +function [Matrix, Vector] = substractRows(Matrix, Vector, i, rowMultiplier, j, Columns) + Vector(i) = Vector(i) - rowMultiplier * Vector(j); + for curentColumn = 1 : Columns + Matrix(i,curentColumn) = Matrix(i,curentColumn) - rowMultiplier * Matrix(j, curentColumn); + end % end for +end % end function + +function [Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector) + for k = Columns : -1 : 1 + % Start at final column and move by -1 each iteration until we reach 1 + equation = 0; + for j = k+1 : Columns + equation = equation + Matrix(k,j) * x(j, 1); + % even though x is a vector we still need to put '1' to ensure + % that number of columns in the first matrix matches number of + % rows in second matrix + end % end for + + x(k, 1) = (Vector(k,1) - equation) / Matrix(k,k); + % even though x is a vector we still need to put '1' to ensure + % that we do not exceed array bounds + end % end for +end % end function + +function x = iterativeResidualCorrection(A, x, b) + r = A*x - b; + for i = 1 : 100 + [A, b, deltaX] = solveSystem(A, r); + newX = x - deltaX; + r = A*newX - b; + x = newX; + end +end % end function diff --git a/ENUME/projectA/indicatedMethod.m b/ENUME/projectA/indicatedMethod.m index 76716419..74728885 100644 --- a/ENUME/projectA/indicatedMethod.m +++ b/ENUME/projectA/indicatedMethod.m @@ -2,9 +2,15 @@ function x = indicatedMethod(Matrix, Vector) % Name of the method as in the text % x stands for obtained result checkIfMatrixIsSquareMatrix(Matrix); [Matrix, Vector, x] = solveSystem(Matrix, Vector); - norm(x) + errorBeforeResidualCorrection = norm(Matrix*x - Vector); x = iterativeResidualCorrection(Matrix, x, Vector); % Improve on the solution - norm(x) + disp("errorBeforeResidualCorrection") + disp(errorBeforeResidualCorrection); + disp("errorAfterResidualCorrection") + disp(norm(Matrix*x - Vector)); + disp("A\b error:") + disp(norm(Matrix * (Matrix \ Vector) - Vector)); + end % end function function [Matrix, Vector, x] = solveSystem(Matrix, Vector) @@ -93,14 +99,13 @@ function [Matrix, Vector, x] = backSubstitutionPhase(Columns, Matrix, Vector) end % end for end % end function + function x = iterativeResidualCorrection(A, x, b) r = A*x - b; - euclideanNormOfR = norm(r); - new_euclideanNormOfR = euclideanNormOfR; - while new_euclideanNormOfR <= euclideanNormOfR - euclideanNormOfR = new_euclideanNormOfR; - r = A*x - b; - x = x - r; - new_euclideanNormOfR = norm(r); - end + for i = 1 : 10 + deltaX = solveSystem(A, r); + newX = x - deltaX; + r = A*newX - b; + x = newX; + end end % end function diff --git a/ENUME/projectReferences/ENUME_Project1 b/ENUME/projectReferences/ENUME_Project1 new file mode 160000 index 00000000..035594f2 --- /dev/null +++ b/ENUME/projectReferences/ENUME_Project1 @@ -0,0 +1 @@ +Subproject commit 035594f28c9fc9c1fcf81a162cb79e6ac9544f56 diff --git a/ENUME/projectReferences/MNUM b/ENUME/projectReferences/MNUM new file mode 160000 index 00000000..50b41b73 --- /dev/null +++ b/ENUME/projectReferences/MNUM @@ -0,0 +1 @@ +Subproject commit 50b41b737ffa54606808b956e3fee9d3f1c0d3e5 diff --git a/ENUME/projectReferences/MNUM_Project_1 b/ENUME/projectReferences/MNUM_Project_1 new file mode 160000 index 00000000..6fa9a4f7 --- /dev/null +++ b/ENUME/projectReferences/MNUM_Project_1 @@ -0,0 +1 @@ +Subproject commit 6fa9a4f7737db7a5949c945059d215d7a46fa0be diff --git a/ENUME/projectReferences/MatLabMNUM b/ENUME/projectReferences/MatLabMNUM new file mode 160000 index 00000000..e60c7bbb --- /dev/null +++ b/ENUME/projectReferences/MatLabMNUM @@ -0,0 +1 @@ +Subproject commit e60c7bbbd49870f0ba297ef29717d0b935643037 diff --git a/ENUME/projectReferences/Metody-Numeryczne-Matlab1 b/ENUME/projectReferences/Metody-Numeryczne-Matlab1 new file mode 160000 index 00000000..c1ea8c47 --- /dev/null +++ b/ENUME/projectReferences/Metody-Numeryczne-Matlab1 @@ -0,0 +1 @@ +Subproject commit c1ea8c47414b4552af22f9eafa5a40c2f03a0607 diff --git a/ENUME/projectReferences/Metody-numeryczne b/ENUME/projectReferences/Metody-numeryczne new file mode 160000 index 00000000..bcfdd828 --- /dev/null +++ b/ENUME/projectReferences/Metody-numeryczne @@ -0,0 +1 @@ +Subproject commit bcfdd828024973cc31d21c8fed20f61a56160ab5 diff --git a/ENUME/projectReferences/Numerical-Methods-metody-numeryczne b/ENUME/projectReferences/Numerical-Methods-metody-numeryczne new file mode 160000 index 00000000..5e872beb --- /dev/null +++ b/ENUME/projectReferences/Numerical-Methods-metody-numeryczne @@ -0,0 +1 @@ +Subproject commit 5e872beb1e00cedcfc73c2712314386155d61310 diff --git a/ENUME/projectReferences/NumericalMethods b/ENUME/projectReferences/NumericalMethods new file mode 160000 index 00000000..3a727d14 --- /dev/null +++ b/ENUME/projectReferences/NumericalMethods @@ -0,0 +1 @@ +Subproject commit 3a727d1411019a175ee619a0b3e08c4be2cc646e diff --git a/ENUME/projectReferences/Semester-5-ENUME b/ENUME/projectReferences/Semester-5-ENUME new file mode 160000 index 00000000..d4889712 --- /dev/null +++ b/ENUME/projectReferences/Semester-5-ENUME @@ -0,0 +1 @@ +Subproject commit d4889712132ca46ad21d1cfc5593db9fd492b0e5 diff --git a/ENUME/projectReferences/metody-numeryczne-3 b/ENUME/projectReferences/metody-numeryczne-3 new file mode 160000 index 00000000..83c97447 --- /dev/null +++ b/ENUME/projectReferences/metody-numeryczne-3 @@ -0,0 +1 @@ +Subproject commit 83c97447715198af5fe2f8478d65341f0adba46a diff --git a/ENUME/projectReferences/metody_numeryczne b/ENUME/projectReferences/metody_numeryczne new file mode 160000 index 00000000..6398ae4e --- /dev/null +++ b/ENUME/projectReferences/metody_numeryczne @@ -0,0 +1 @@ +Subproject commit 6398ae4e5f5c7c9bd94d75180a4eae0a93306727 diff --git a/ENUME/projectReferences/numerical-methods b/ENUME/projectReferences/numerical-methods new file mode 160000 index 00000000..7b85cc99 --- /dev/null +++ b/ENUME/projectReferences/numerical-methods @@ -0,0 +1 @@ +Subproject commit 7b85cc9941433b3b1148bb6e6b1b7d117f7a72a4 diff --git a/ENUME/projectReferences/report_A.pdf b/ENUME/projectReferences/report_A.pdf new file mode 100644 index 00000000..1de9cd3c Binary files /dev/null and b/ENUME/projectReferences/report_A.pdf differ diff --git a/ENUME/projectReferences/szopinski-enume b/ENUME/projectReferences/szopinski-enume new file mode 160000 index 00000000..3a736998 --- /dev/null +++ b/ENUME/projectReferences/szopinski-enume @@ -0,0 +1 @@ +Subproject commit 3a7369980bccf1e7afcb323fd5f7077dfeb89b1a