function [E , steps] = myqrmethod (A) % Computes all the eigenvalues of a matrix
ID: 3210080 • 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
function dummy=myqrmethod1()
clc;
clear all;
format long
format compact
z=[10 50 200];
for s=1:3
A=hilb(z(s));
N=z(s)
[Eqr, steps]=myqrmethod(A);
Eml=eig(A);
diff=norm(Eml-flipud(Eqr))
end
function [E, steps]=myqrmethod(A)
[m n]=size(A);
if (m ~=n)
warning('the input matrix is not square')
return
end
Max_step=1000;
H=hess(A);
E=diag(H);
change=1;
steps=0;
while(steps<Max_step)
Eold=E;
[Q R]=qr(H);
H=R*Q;
E=diag(H);
change=norm(E-Eold);
steps=steps+1;
end
end
end
%%% Solution'
N =
10
diff =
1.375210822846235e-15
N =
50
diff =
4.168496863958721e-15
N =
200
diff =
1.589990599203073e-14
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.