-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.m
96 lines (82 loc) · 2.58 KB
/
test.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
timeMatrix = zeros(5,20);
answerMatrix = zeros(5,20);
counterMatrix = zeros(5,20);
numTimes = 5000;
counter = 1;
eigenValues = zeros(2,20);
for i=100:100:2000
disp('Working on')
i
% Generate a random hermitian matrix
A = randn(i,i);
A = (A + A')/2;
while cond(A) > 5e4
A = randn(i,i);
A = (A + A')/2;
end
% find the dominant, and second most dominant eigenvalue
temp = eig(A);
eigenValues(1,counter) = temp(length(temp));
eigenValues(2,counter) = temp(length(temp)-1);
% Matlab's eig function
timeStart = tic;
disp('eig')
answerMatrix(1,counter) = max(eig(A));
timeMatrix(1,counter) = toc(timeStart);
% PowerIteration
timeStart = tic;
disp('power')
[answerMatrix(2,counter) garbage counterMatrix(2,counter)] = powerIteration(A,numTimes);
timeMatrix(2,counter) = toc(timeStart);
% InverseIteration
timeStart = tic;
disp('inverse')
[answerMatrix(3,counter) garbage counterMatrix(3,counter)] = inverseIteration(A,numTimes);
timeMatrix(3,counter) = toc(timeStart);
% Rayleigh
timeStart = tic;
disp('rayleigh')
[answerMatrix(4,counter) garbage counterMatrix(4,counter)] = rayleigh(A,numTimes);
timeMatrix(4,counter) = toc(timeStart);
% SinglePass
timeStart = tic;
disp('singlepass')
answerMatrix(5,counter) = singlePass(A,i);
counterMatrix(5,counter) = 1;
timeMatrix(5,counter) = toc(timeStart);
counter = counter + 1;
end
% Graphing time and number of iterations
semilogy(100:100:2000, timeMatrix(1,:))
hold on
semilogy(100:100:2000, timeMatrix(2,:), 'r')
hold on
semilogy(100:100:2000, timeMatrix(3,:), 'g')
hold on
semilogy(100:100:2000, timeMatrix(4,:), 'm')
hold on
semilogy(100:100:2000, timeMatrix(5,:), 'k')
title ('semilogy Time Elapsed till Convergence')
xlabel('Matrix Size x by x')
ylabel('Time')
legend('Built-in Matlab Eigenvalue Solver','Power Iteration','Inverse Iteration','Rayleigh','Single Pass')
figure
plot(100:100:2000, timeMatrix(1,:))
hold on
plot(100:100:2000, timeMatrix(2,:), 'r')
hold on
semilogy(100:100:2000, timeMatrix(3,:), 'g')
hold on
plot(100:100:2000, timeMatrix(4,:), 'm')
hold on
plot(100:100:2000, timeMatrix(5,:), 'k')
title ('Time Elapsed till Convergence')
xlabel('Matrix Size x by x')
ylabel('Time')
legend('Built-in Matlab Eigenvalue Solver','Power Iteration','Inverse Iteration','Rayleigh','Single Pass')
figure
plot(100:100:2000, eigenValues)
title('eigenvalues of the matrix')
ylabel('eigenvalue')
xlabel('Matrix Size x by x')
legend('largest eigenvalue', 'second largest eigenvalue')