function exercise11task1 A=[2 1 0; 1 3 -1; 0 -1 1]; v_0 = [1 0 0]'; % COMPUTATION OF THE LARGEST EIGENVALUE % vector iteration with the residual as stop criterion: v=v_0; r1 = v'*A*v; rayleigh1 = [r1]; while norm(A*v - r1*v)>5*eps v = A*v; v = v / norm(v); r1 = v'*A*v; % approximation of the Eigenvalue via Rayleigh-quotient rayleigh1 = [rayleigh1,r1]; end disp('the largest eigenvalue is:') lambda = r1 % COMPUTATION OF THE SMALLEST EIGENVALUE % inverse iteration: v=v_0; r2 = v'*A*v; rayleigh2 = [r2]; while norm(A*v - r2*v)>5*eps v = A\v; v = v / norm(v); r2 = v'*A*v; % approximation of the Eigenvalue via Rayleigh-quotient rayleigh2 = [rayleigh2,r2]; end disp('the smallest eigenvalue is:') lambda = r2 % COMPUTATION OF THE LAST EIGENVALUE % inverse iteration with shift: v=v_0; B = A - 1.65*eye(3); % shift = 2 r3 = v'*A*v; rayleigh3 = [r3]; while norm(A*v - r3*v)>5*eps v = B\v; v = v / norm(v); r3 = v'*A*v; % approximation of the Eigenvalue via Rayleigh-quotient rayleigh3 = [rayleigh3,r3]; end disp('the last eigenvalue is:') lambda = r3 % PLOT: semilogy(1:length(rayleigh1),abs(rayleigh1-r1),'r',1:length(rayleigh2),abs(rayleigh2-r2),'b',1:length(rayleigh3),abs(rayleigh3-r3),'k') legend('largest eigenvalue','smallest eigenvalue','last eigenvalue')