Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

Power Method Algorithm using MATLAB(m-file)

Finding eigenvalue and eigenvector



MATLAB Program:

% Power Method Algorithm

 n=input('Enter dimension of the matrix, n:  ');
 A = zeros(n,n);
 x = zeros(1,n);
 y = zeros(1,n);
 tol = input('Enter the tolerance, tol: ');
 m = input('Enter maximum number of iterations, m:  ');


 A=[1 2 0; -2 1 2; 1 3 1];
 x=[1 1 1];


 k = 1; lp = 1;
 amax = abs(x(1));
 for i = 2 : n
    if abs(x(i)) > amax
        amax = abs(x(i));
        lp = i;
    end
 end 
 for i = 1 : n
    x(i) = x(i)/amax;
  end 

 fprintf('\n\n  Ite.    Eigenvalue     ............Eigenvectores............\n');
 while k <= m 
      for i = 1 : n
         y(i) = 0;
         for j = 1 : n
           y(i) = y(i) + A(i,j) * x(j);
         end
      end
      ymu = y(lp);
      lp = 1;
      amax = abs(y(1));
      for i = 2 : n
         if abs(y(i)) > amax
            amax = abs(y(i));
            lp = i;
         end
      end
      if amax <= 0
         fprintf('0 eigenvalue - select another ');
         fprintf('initial vector and begin again\n');
      else
           err = 0;
           for i = 1 : n
              t = y(i)/y(lp);
              if abs(x(i)-t) > err
                err = abs(x(i)-t);
              end
              x(i) = t;
           end
           fprintf('%4d     %11.8f', k, ymu);
           for i = 1 : n
             fprintf('   %11.8f', x(i));
           end
           fprintf('\n');
           if err <= tol
              fprintf('\n\nThe eigenvalue after %d iterations is: %11.8f \n',k, ymu);
              fprintf('The corresponding eigenvector is: \n');
              for i = 1 : n
                 fprintf('                                 %11.8f \n', x(i));
              end
              fprintf('\n');
              break;
           end
           k = k+1;
       end
 end
 if k > m
 fprintf('Method did not converge within %d iterations\n', m);
 end

OUTPUTS:

>> power_C
Enter dimension of the matrix, n:  3
Enter the tolerance, tol: 0.00001
Enter maximum number of iterations, m:  100


  Ite.    Eigenvalue     ............Eigenvectores............
   1      3.00000000    0.60000000    0.20000000    1.00000000
   2      2.20000000    0.45454545    0.45454545    1.00000000
   3      2.81818182    0.48387097    0.54838710    1.00000000
   4      3.12903226    0.50515464    0.50515464    1.00000000
   5      3.02061856    0.50170648    0.49488055    1.00000000
   6      2.98634812    0.49942857    0.49942857    1.00000000
   7      2.99771429    0.49980938    0.50057186    1.00000000
   8      3.00152497    0.50006351    0.50006351    1.00000000
   9      3.00025403    0.50002117    0.49993650    1.00000000
  10      2.99983066    0.49999294    0.49999294    1.00000000

  11      2.99997177    0.49999765    0.50000706    1.00000000
  12      3.00001882    0.50000078    0.50000078    1.00000000


The eigenvalue after 12 iterations is:  3.00001882 
The corresponding eigenvector is: 
                                  0.50000078 
                                  0.50000078 

                                  1.00000000 



No comments

Popular Posts

Followers