-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPower_Method_forEigenValueNVectors.m
73 lines (65 loc) · 1.81 KB
/
Power_Method_forEigenValueNVectors.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
clear;clc
% Power Method Algorithm
A=input('Input matrix: ')
x=input('Input Initial approximation as row vector: ');
n=length(x);
y = zeros(1,n);
tol = input('Enter the tolerance, tol: ');
m = input('Enter maximum number of iterations, m: ');
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
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 = max(y);
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