Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- name=input('Enter name of input text file : ','s');
- %name='in_2';
- name=strcat(name,'.txt');
- fileid=fopen(name, 'r');
- formatspec= '%f';
- sizea=inf;
- A = fscanf(fileid,formatspec,sizea);
- fclose(fileid);
- n=A(1);
- mat=(reshape(A(2:n*n+1),n,n))';
- tol=A(n*n+2);
- choose=input('what do you need? (l/a) \nl-largest eigenvalue \na-all eigenvalues \n','s');
- if choose=='l'
- [l,v,i]=powme(mat,n,tol)
- fileid=fopen('out_2l.txt','w');
- fprintf(fileid,'Largest eigenvalue \n');
- fprintf(fileid,'%f\n',l);
- fprintf(fileid,'Eigenvectors \n');
- fprintf(fileid,'%f\n',v);
- fprintf(fileid,'No. of iterations\n%d\n',i);
- fprintf(fileid,'\n');
- fclose(fileid);
- end
- if choose=='a'
- [ev,i]=qrev(mat,n,tol)
- fileid=fopen('out_2a.txt','w');
- fprintf(fileid,'Eigenvalues \n');
- fprintf(fileid,'%f\n',ev);
- fprintf(fileid,'No. of iterations\n%d\n',i);
- fclose(fileid);
- end
- function [l,v,i]=powme(mat,n,tol)
- z=zeros(n,1);
- z(1)=1;
- y=z;
- i=0;
- while 1
- i=i+1;
- y=z/norm(z,2);
- z=mat*y;
- yo=y;
- y=z/norm(z,2);
- z=mat*y;
- if norm(y-yo,Inf)*100<=tol
- break
- end
- end
- l=(y')*z;
- v=y;
- end
- function [ev,ic]=qrev(mat,n,tol)
- ic=0;
- while 1
- ic=ic+1;
- q=zeros(n,n);
- z=zeros(n,1);
- q(:,1)=mat(:,1)/norm(mat(:,1),2);
- for k=1:n-1
- sum=zeros(n,1);
- for i=1:k
- sum=sum+((mat(:,k+1)')*q(:,i))*q(:,i);
- end
- z=mat(:,k+1)-sum;
- q(:,k+1)=z/norm(z,2);
- end
- r=q'*mat;
- lo=diag(mat);
- mat=r*q;
- l=diag(mat);
- if norm(l-lo,Inf)*100<=tol
- break;
- end
- end
- ev=l;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement