-
Notifications
You must be signed in to change notification settings - Fork 0
/
LanczosCGS.m
63 lines (51 loc) · 1.03 KB
/
LanczosCGS.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
function LanczosCGS(A,v,k)
T = zeros(k+1,k);
%first iteration
v = v/norm(v); V = v;
w = A*v; a(1) = v'*w;
w = w - v*a(1); b(2) = norm(w);
v = w/b(2); V = [V v];
%forming of first column of T
t = [a(1) b(2) zeros(1,k-1)];
T(:,1) = t';
%solving eigenvalue problem
[y,mu] = eig(T(1,1));
Mu=[1];
Mu = [Mu, mu(1,1)];
r = [abs(T(2,1))*abs(y(1,1))];
%creating table (output)
Table=[Mu; T(k+1,k) r];
disp(Table)
%iterations 2 - k
for j=2:k
r = [];
%Lanczos iteration
w = A*v;
a(j) = v'*w;
w = w - b(j)*V(:,j-1)-a(j)*v;
b(j+1) = norm(w);
v = w/b(j+1);
V = [V v];
%forming of rest of the columns of T
t = [zeros(1,j-2) b(j) a(j) b(j+1) zeros(1,k-j)];
T(:,j) = t';
%solving eigenvalue problem
[y,mu] = eig(T(1:j,1:j));
Mu=[j];
for i=1:j
Mu = [Mu, mu(i,i)];
r = [r,abs(T(i+1,i))*abs(y(i,i))];
end
%creating table (output)
Table=[Mu; T(k+1,k) r];
disp(Table)
end
for n=1:j
Plot(j,n)=Table(1,n+1);
end
end
color = rand(k,3);
for t=1:k
plot(t,Plot(t,1:t), '-*','MarkerEdgeColor', color(t,:));
hold on;
end