function aleig

function f=aleig(a,b,eps)
% find the fondamental eigenvalue and corresponding eigenvector of
% equation (a-eval*b)*evect=0 using the inverse power method.
% function f=aleig(a,b,eps)
%
try
a=inv(a) ;
catch
error('singular matrix')
end
% perform power iterations.
a=a*b ;
iter=0 ;
eval=0 ;
n=size(a,2) ;
evect=ones(n,1) ;
while true
iter=iter+1 ;
if iter > 300
error('unable to converge(1).')
end
gar=a*evect ;
s1=sum(gar.*evect) ;
s2=sum(gar.*gar) ;
zz=abs(eval-s1/s2) ;
eval=s1/s2 ;
err1=max(abs(gar*eval)) ;
err2=max(abs(gar*eval-evect)) ;
evect=gar*eval ;
if zz <= eps & err2 <= err1*eps
f{1}=iter ;
f{2}=eval ;
f{3}=evect ;
break
elseif iter == 1
test=zz ;
elseif iter >= 10 & zz > test
error('unable to converge(2)')
end
end