function [E , steps] = myqrmethod (A) % Computes all the eigenvalues of a matrix
ID: 3196619 • Letter: F
Question
function [E , steps] = myqrmethod (A) % Computes all the eigenvalues of a matrix using the QR method. % Input: A-square matrix % Outputs: -vector of eigenvalues steps - the number of iterations it took m n]- size (A); warning('The input matrix is not square.') return end % Set up initial estimate H = hess (A) ; E diag(H); change = 1; steps 0; % loop while estimate changes while change> 0 Eold - E; % apply QR method E = diag(H); % test change change -norm (E Eold); steps = steps +1; end end As you can see the main steps of the program are very simple. The really hard calculations are contained in the built-in commands hess(A) and qr (H) Run this program and compare the results with MATLAB's built in command format long format compact Eqr, steps] myqrmethod (A) Emleig(A) diff norm (Eml - flipud (Eqr ) ) Exercises 17.1 Modify myqrmethod to stop after 1000 iterations. Use the modified program on the matrix A - hilb(n) with n equal to 10, 50, and 200. Use the norm to compare the results to the eigenvalues obtained from MATLAB's built-in program eig. Turn in a printout of your program and a brief report on the experimentExplanation / Answer
a) To stop after 100 interations, add a line like shown:
while change > 0
if steps < 101
Eold = E;
[Q R] = qr(H);
H = R*Q;
E = diag(H);
change = norm(E - Eold);
steps = steps + 1;
end
end
This makes sure that steps do not cross 100.
When n = 10;
code's result:
ans =
1.751919670265179
0.342929548483509
0.035741816271639
0.002530890768670
0.000128749614276
0.000004729689293
0.000000122896774
0.000000002147439
0.000000000022667
0.000000000000109
inbuilt function's result:
0.000000000000109
0.000000000022667
0.000000002147439
0.000000122896774
0.000004729689293
0.000128749614276
0.002530890768670
0.035741816271639
0.342929548483509
1.751919670265178
There is just an inversion of eigen values according to the way we've written our code, but almost all the values match, almost till the last digit
for n = 50 and 200, it takes so much time to complete the calculation and anywayws, it'll be column vectors of size 50 and 200 respectively
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.