I need a Matlab code for this algorithm, and this is the Power Method INPUT dime
ID: 3685089 • Letter: I
Question
I need a Matlab code for this algorithm, and this is the Power Method
INPUT dimension n; matrix A; vector x; tolerance TOL; maximum number of iterations N.
Step 1: set k=1
step 3: set x=x/xp
step 5: set y=Ax
step 6: set u=yp
step 8 if yp=0 then OUTPUT ('Eigenvector',x)
OUTPUT ('A has the eigenvalue 0, select a new vector x and restart')
STOP
x=y/yp
step 10: if ERR<TOL then OUTPUT (u,x);
(the procedure was successful.)
STOP.
step11 set k=k+1
step 12 OUTPUT('The maximum number of iterations exceeded');
(The procedure was unsuccessful.)
STOP
Explanation / Answer
To approximate the dominant eigenvalue and an associatedeigenvector of the n by n matrix A given a nonzero vector x:
INPUT: Dimension n; matrix A; vector x; tolerance TOL; maximum number of iterations N.
OUTPUT: Approximate eigenvalue U; approximate eigenvector x or a message that the maximum number of iterations was exceeded.
syms('OK', 'AA', 'NAME', 'INP', 'N', 'I', 'J', 'A', 'X', 'TOL');
syms('NN', 'FLAG', 'OUP', 'K', 'LP', 'AMAX', 'Y', 'YMU');
syms('ERR', 'T');
TRUE = 1;
FALSE = 0;
fprintf(1,'This is the Power Method. ');
OK = FALSE;
fprintf(1,'The array will be input from a text file in the order: ');
fprintf(1,'A(1,1), A(1,2), ..., A(1,n), ');
fprintf(1,'A(2,1), A(2,2), ..., A(2,n), ');
fprintf(1,'..., A(n,1), A(n,2), ..., A(n,n) ');
fprintf(1,'Place as many entries as desired on each line, but ');
fprintf(1,'separate entries with ');
fprintf(1,'at least one blank. ');
fprintf(1,'The initial approximation should follow in same
format. ');
fprintf(1,'Has the input file been created? - enter Y or N. ');
AA = input (' ','s');
if AA == 'Y' | AA == 'y'
fprintf(1,'Input the file name in the form - drive:name.ext ');
fprintf(1,'for example: A:DATA.DTA ');
NAME = input(' ','s');
INP = fopen(NAME,'rt');
OK = FALSE;
while OK == FALSE
fprintf(1,'Input the dimension n. ');
N = input(' ');
if N > 0
A = zeros(N,N);
X = zeros(1,N);
Y = zeros(1,N);
for I = 1 : N
for J = 1 : N
A(I,J) = fscanf(INP, '%f',1);
end;
end;
for I = 1 : N
X(I) = fscanf(INP, '%f',1);
end;
fclose(INP);
while OK == FALSE
fprintf(1,'Input the tolerance. ');
TOL = input(' ');
if TOL > 0
OK = TRUE;
else
fprintf(1,'Tolerance must be positive number. ');
end;
end;
OK = FALSE;
while OK == FALSE
fprintf(1,'Input maximum number of iterations ');
fprintf(1,'- integer. ');
NN = input(' ');
% use NN for N
if NN > 0
OK = TRUE;
else
fprintf(1,'Number must be positive integer. ');
end;
end;
else
fprintf(1,'The dimension must be a positive integer. ');
end;
end;
else
fprintf(1,'The program will end so the input file can be created. ');
end;
if OK == TRUE
fprintf(1,'Choice of output method: ');
fprintf(1,'1. Output to screen ');
fprintf(1,'2. Output to text file ');
fprintf(1,'Please enter 1 or 2. ');
FLAG = input(' ');
if FLAG == 2
fprintf(1,'Input the file name in the form - drive: ame.ext ');
fprintf(1,'for example A:\OUTPUT.DTA ');
NAME = input(' ','s');
OUP = fopen(NAME,'wt');
else
OUP = 1;
end;
fprintf(OUP, 'POWER METHOD ');
fprintf(OUP, 'iter approx approx eigenvector ');
fprintf(OUP, ' eigenvalue ');
% STEP 1
K = 1;
% STEP 2
LP = 1;
AMAX = abs(X(1));
for I = 2 : N
if abs(X(I)) > AMAX
AMAX = abs(X(I));
LP = I;
end;
end;
% STEP 3
for I = 1 : N
X(I) = X(I)/AMAX;
end;
% STEP 4
while K <= NN & OK == TRUE
% STEP 5
for I = 1 : N
Y(I) = 0;
for J = 1 : N
Y(I) = Y(I) + A(I,J) * X(J);
end;
end;
% STEP 6
YMU = Y(LP);
% STEP 7
LP = 1;
AMAX = abs(Y(1));
for I = 2 : N
if abs(Y(I)) > AMAX
AMAX = abs(Y(I));
LP = I;
end;
end;
% STEP 8
if AMAX <= 0
fprintf(1,'0 eigenvalue - select another ');
fprintf(1,'initial vector and begin again ');
OK = FALSE;
else
% STEP 9
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(OUP, '%d %12.8f', K, YMU);
for I = 1 : N
fprintf(OUP, ' %11.8f', X(I));
end;
fprintf(OUP, ' ');
% STEP 10
if ERR <= TOL
fprintf(OUP, ' The eigenvalue = %12.8f',YMU);
fprintf(OUP, ' to tolerance = %.10e ', TOL);
fprintf(OUP, 'obtained on iteration number = %d ', K);
fprintf(OUP, 'Unit eigenvector is : ');
for I = 1 : N
fprintf(OUP, ' %11.8f', X(I));
end;
fprintf(OUP, ' ');
OK = FALSE;
end;
% STEP 11
K = K+1;
end;
end;
% STEP 12
if K > NN
fprintf(1,'Method did not converge within %d iterations ', NN);
end;
if OUP ~= 1
fclose(OUP);
fprintf(1,'Output file %s created successfully ',NAME);
end;
end;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.