-
Notifications
You must be signed in to change notification settings - Fork 0
/
Excersise7.m
77 lines (63 loc) · 1.32 KB
/
Excersise7.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
A = diag([1:10]);
v = ones(10,1);
k = 8;
[V,H] = ArnoldiMGS(A,v,k);
[W,H2] = FilterAway(2,V,H);
R = A*W(:,1:k-1)-W*H2;
e = eig(H2(1:k-1,1:k-1));
[V2,H3] = ExtendArnoldi(A,V,H);
R = A*V2(:,1:k+1)-V2*H3;
OrthoTest = V2*V2';
ListRitzData(H3)
function [W,H2] = FilterAway(mu,V,H)
size_h = size(H);
k = size_h(2);
[Q,R] = qr(H- mu*eye(k+1,k),0);
W = V*Q;
H2 = R*Q(1:k,1:k-1)+mu*eye(k,k-1);
end
function [V2,H2] = ExtendArnoldi(A,V,H)
v = V(:,end);
w = A*v;
q = w;
for l = 1:size(V,2)
w = w-V(:,l)*(V(:,l)'*w);
end
g = norm(w);
v = w/g;
h = V'*q;
V2 = [V v];
H2 = [H,h; zeros(1,size(V,2)-1),g];
end
function L = ListRitzData(H)
size_H = size(H);
[V,D] = eig(H(1:size_H(2),1:size_H(2)));
k = size_H(2);
h = H(k+1,k);
ResultTable = [k; abs(h)];
for j = 1 : k
eigenVec = V(:,j);
r = abs(h*eigenVec(k));
eigenVal = D(j,j);
newColumn = [eigenVal;r];
ResultTable = [ResultTable newColumn];
end
display(ResultTable)
end
function [V,H] = ArnoldiMGS(A,v,k)
v = v/norm(v);
V = v; H = [];
n = size(v);
for j=1:k
w = A*v;
q = w;
for l = 1:size(V,2)
w = w-V(:,l)*(V(:,l)'*w);
end
g = norm(w);
v = w/g;
h = V'*q;
V = [V v];
H = [H,h; zeros(1,j-1),g];
end
end