adding references

This commit is contained in:
PolishPigeon 2021-11-10 07:23:22 +01:00
parent c92984e9f5
commit 8655e766a6
16 changed files with 145 additions and 10 deletions

View File

@ -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

View File

@ -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

@ -0,0 +1 @@
Subproject commit 035594f28c9fc9c1fcf81a162cb79e6ac9544f56

@ -0,0 +1 @@
Subproject commit 50b41b737ffa54606808b956e3fee9d3f1c0d3e5

@ -0,0 +1 @@
Subproject commit 6fa9a4f7737db7a5949c945059d215d7a46fa0be

@ -0,0 +1 @@
Subproject commit e60c7bbbd49870f0ba297ef29717d0b935643037

@ -0,0 +1 @@
Subproject commit c1ea8c47414b4552af22f9eafa5a40c2f03a0607

@ -0,0 +1 @@
Subproject commit bcfdd828024973cc31d21c8fed20f61a56160ab5

@ -0,0 +1 @@
Subproject commit 5e872beb1e00cedcfc73c2712314386155d61310

@ -0,0 +1 @@
Subproject commit 3a727d1411019a175ee619a0b3e08c4be2cc646e

@ -0,0 +1 @@
Subproject commit d4889712132ca46ad21d1cfc5593db9fd492b0e5

@ -0,0 +1 @@
Subproject commit 83c97447715198af5fe2f8478d65341f0adba46a

@ -0,0 +1 @@
Subproject commit 6398ae4e5f5c7c9bd94d75180a4eae0a93306727

@ -0,0 +1 @@
Subproject commit 7b85cc9941433b3b1148bb6e6b1b7d117f7a72a4

Binary file not shown.

@ -0,0 +1 @@
Subproject commit 3a7369980bccf1e7afcb323fd5f7077dfeb89b1a