Implements power method with shifts to find an eigenvector of A, and its corresponding eigenvalue. Input arguments: A, square matrix s, scalar shift tol, tolerance Output arguments: v, eigenvector of A c, corresponding eigenvalue k, number of iterations
0001 function [ v,c,k ] = shifted_power( A,s,tol ) 0002 % Implements power method with shifts to find an eigenvector of A, 0003 % and its corresponding eigenvalue. 0004 % Input arguments: 0005 % A, square matrix 0006 % s, scalar shift 0007 % tol, tolerance 0008 % Output arguments: 0009 % v, eigenvector of A 0010 % c, corresponding eigenvalue 0011 % k, number of iterations 0012 0013 [n,m]=size(A); % finding the size of A 0014 if n~= m; 0015 error('input A is not a square matrix'); 0016 elseif tol<=0; 0017 error('tolerance should be positive'); 0018 end 0019 0020 x0=rand(n,1); % initializing x to a random vector 0021 k=0; % initializing k 0022 0023 while k>=0; 0024 B = (A-s*eye(n)); 0025 x1=B*x0; % apply (A-sI) to generate new estimate 0026 % of eigenvector 0027 rayleigh=x0'*B*x0/(x0'*x0); % calculate Rayleigh coefficient 0028 0029 y=x1-rayleigh*x0; % if |y|<tol we terminate procedure 0030 if sqrt(y'*y)<tol; 0031 c=rayleigh+s; % accept rayleigh+s as eigenvalue 0032 v=x0; % accept x0 as eigenvector 0033 return; % return function 0034 else 0035 x0=x1/sqrt(x1'*x1); % normalize x 0036 k=k+1; % increment k 0037 end 0038 end 0039 0040 0041 end 0042