mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 14:43:08 +02:00
Fixing QRNoShifts
This commit is contained in:
parent
903d1529bb
commit
fc66144833
@ -1,17 +1,8 @@
|
||||
function [eigenValues, whichIterationAreWeOn, Matrix] = QRNoShifts(Matrix)
|
||||
startingMatrix = Matrix;
|
||||
matlabEigen = eig(Matrix);
|
||||
[whichIterationAreWeOn, threshold] = initializeValues();
|
||||
while threshold > 1e-6
|
||||
[Q, R] = gramSchmidtAlgorithm(Matrix);
|
||||
Matrix = R * Q;
|
||||
whichIterationAreWeOn = whichIterationAreWeOn + 1;
|
||||
|
||||
% iterate until all non-diagonal elements are below the threshold
|
||||
nondiag = Matrix - diag(diag(Matrix)); % first diag converts Matrix
|
||||
% into vector consisting of values on the diagonal of matrix,
|
||||
% second diag converts this vector into matrix with zeros on
|
||||
% everything except diagonal
|
||||
threshold = max(abs(nondiag));
|
||||
end
|
||||
[Matrix, whichIterationAreWeOn, eigenValues] = QRNoShiftsLoop(threshold, Matrix, whichIterationAreWeOn);
|
||||
|
||||
% convert eigenvalue matrix to vector
|
||||
eigenValues = diag(Matrix)';
|
||||
@ -21,6 +12,30 @@ function [eigenValues, whichIterationAreWeOn, Matrix] = QRNoShifts(Matrix)
|
||||
disp(Matrix);
|
||||
end
|
||||
|
||||
function [Matrix, whichIterationAreWeOn, eigenValues] = QRNoShiftsLoop(threshold, Matrix, whichIterationAreWeOn)
|
||||
while threshold > 1e-6
|
||||
[Q, R] = gramSchmidtAlgorithm(Matrix);
|
||||
Matrix = R * Q;
|
||||
whichIterationAreWeOn = whichIterationAreWeOn + 1;
|
||||
|
||||
% iterate until all non-diagonal elements are below the threshold
|
||||
matrixWithoutDiagonal = Matrix - diag(diag(Matrix)); % first diag converts Matrix
|
||||
% into vector consisting of values on the diagonal of matrix,
|
||||
% second diag converts this vector into matrix with zeros on
|
||||
% everything except diagonal
|
||||
% If we substract it from Matrix we get original Matrix with zeros
|
||||
% on a diagonal
|
||||
threshold = max(abs(matrixWithoutDiagonal));
|
||||
end
|
||||
end
|
||||
|
||||
function displayResults(eigenValues, whichIterationsAreWeOn, Matrix, startingMatrix, matlabEigen)
|
||||
disp("How many iterations it took:")
|
||||
disp(whichIterationsAreWeOn);
|
||||
disp("Starting matrix:")
|
||||
disp(M)
|
||||
end
|
||||
|
||||
function [whichIterationAreWeOn, threshold] = initializeValues()
|
||||
whichIterationAreWeOn = 0;
|
||||
threshold = inf;
|
||||
|
||||
@ -1,31 +1,51 @@
|
||||
function [eigenValues, whichIterationAreWeOn, Matrix] = QRNoShifts(Matrix)
|
||||
[whichIterationAreWeOn, threshold] = initializeValues();
|
||||
while threshold > 1e-6
|
||||
[Q, R] = gramSchmidtAlgorithm(Matrix);
|
||||
Matrix = R * Q;
|
||||
whichIterationAreWeOn = whichIterationAreWeOn + 1;
|
||||
|
||||
% iterate until all non-diagonal elements are below the threshold
|
||||
matrixWithoutDiagonal = Matrix - diag(diag(Matrix)); % first diag converts Matrix
|
||||
% into vector consisting of values on the diagonal of matrix,
|
||||
% second diag converts this vector into matrix with zeros on
|
||||
% everything except diagonal
|
||||
% If we substract it from Matrix we get original Matrix with zeros
|
||||
% on a diagonal
|
||||
threshold = max(abs(matrixWithoutDiagonal));
|
||||
end
|
||||
|
||||
% convert eigenvalue matrix to vector
|
||||
|
||||
[whichIterationAreWeOn, threshold, startingMatrix, matlabEigen] = initializeValues(Matrix);
|
||||
[Matrix, whichIterationAreWeOn] = QRNoShiftsLoop(threshold, Matrix, whichIterationAreWeOn);
|
||||
eigenValues = diag(Matrix)';
|
||||
disp("Iterations it took: ");
|
||||
disp(whichIterationAreWeOn);
|
||||
disp("Final matrix: ");
|
||||
disp(Matrix);
|
||||
displayResults(whichIterationAreWeOn, Matrix, startingMatrix, matlabEigen);
|
||||
end
|
||||
|
||||
function [whichIterationAreWeOn, threshold] = initializeValues()
|
||||
function [Matrix, whichIterationAreWeOn] = QRNoShiftsLoop(threshold, Matrix, whichIterationAreWeOn)
|
||||
while threshold > 1e-6
|
||||
[Matrix, whichIterationAreWeOn, threshold] = QRNoShiftsInsideLoop(Matrix, whichIterationAreWeOn);
|
||||
end
|
||||
end
|
||||
|
||||
function [Matrix, whichIterationAreWeOn, threshold] = QRNoShiftsInsideLoop(Matrix, whichIterationAreWeOn)
|
||||
[Q, R] = gramSchmidtAlgorithm(Matrix);
|
||||
Matrix = R * Q;
|
||||
whichIterationAreWeOn = whichIterationAreWeOn + 1;
|
||||
|
||||
% iterate until all non-diagonal elements are below the threshold
|
||||
matrixWithoutDiagonal = Matrix - diag(diag(Matrix)); % first diag converts Matrix
|
||||
% into vector consisting of values on the diagonal of matrix,
|
||||
% second diag converts this vector into matrix with zeros on
|
||||
% everything except diagonal
|
||||
% If we substract it from Matrix we get original Matrix with zeros
|
||||
% on a diagonal
|
||||
threshold = max(max(abs(matrixWithoutDiagonal)));
|
||||
% first max returns vector of elements
|
||||
% second max returns max element from this vector
|
||||
end
|
||||
|
||||
function displayResults(whichIterationsAreWeOn, Matrix, startingMatrix, matlabEigen)
|
||||
disp("How many iterations it took:")
|
||||
disp(whichIterationsAreWeOn)
|
||||
disp("Starting Matrix:")
|
||||
disp(startingMatrix)
|
||||
disp("Final Matrix:")
|
||||
disp(Matrix)
|
||||
disp("eig(Matrix) eigen values:")
|
||||
disp(matlabEigen)
|
||||
disp("Our eigen values:")
|
||||
end
|
||||
|
||||
function [whichIterationAreWeOn, threshold, startingMatrix, matlabEigen] = initializeValues(Matrix)
|
||||
whichIterationAreWeOn = 0;
|
||||
threshold = inf;
|
||||
startingMatrix = Matrix;
|
||||
matlabEigen = eig(Matrix);
|
||||
end
|
||||
|
||||
% performs QR or QRdash decomposition of a matrix
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
% finds the eigenValues of a matrix using QR with shifts
|
||||
function [eigenValues, whatIterationAreWeOn, Matrix] = QRShifts(Matrix)
|
||||
eigenFromMatlab = eig(Matrix);
|
||||
initialMatrix = Matrix;
|
||||
[eigenValues, whatIterationAreWeOn, matrixSize, minThreshold] = initiateValues(Matrix);
|
||||
[Matrix, whatIterationAreWeOn, eigenValues] = QRShiftLoop(matrixSize, Matrix, eigenValues, minThreshold, whatIterationAreWeOn);
|
||||
dispResults(Matrix, whatIterationAreWeOn, eigenFromMatlab);
|
||||
dispResults(Matrix, whatIterationAreWeOn, eigenFromMatlab, initialMatrix);
|
||||
end
|
||||
|
||||
function [eigenValues, whatIterationAreWeOn, matrixSize, minThreshold] = initiateValues(Matrix)
|
||||
@ -121,7 +122,9 @@ function [Q, R] = normalizeColumns(columns, Q, R)
|
||||
end
|
||||
end
|
||||
|
||||
function dispResults(Matrix, whatIterationAreWeOn, eigenFromMatlab)
|
||||
function dispResults(Matrix, whatIterationAreWeOn, eigenFromMatlab, initialMatrix)
|
||||
disp("Initial matrix: ");
|
||||
disp(initialMatrix);
|
||||
disp("Final matrix: ");
|
||||
disp(Matrix);
|
||||
disp("Number of iterations: ");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user