forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol12.tar
198 lines (147 loc) · 10 KB
/
sol12.tar
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
ex12_2.m 0000644 0001751 0001751 00000003754 13213235717 011577 0 ustar arbenz arbenz % Plot the figure of the relative residual vs. the condition number of
% projected matrix T.
% The figure shows how the condition number affects the convergence
% behaviour. We find that the BiCG solver is less stable than GMRES. The
% condition number of T is increasing during iterations, consequently the
% relative residual may also increase at certain iteration steps.
[A,b] = HeatFlowSolver2D;
condest(A)
n = size(A,2);
b = ones(n,1);
%You can accurately solve A*x = b
x = A \ b;
norm(b-A*x) / norm(b);
maxit=1000;
droptol=.3;
setup.type='ilutp';
setup.droptol=droptol
[L,U] = ilu(A,setup);
%Now try to solve A*x = b with different unsymmetric solvers.
tic
[x,flag,relres,iter,resvec] = bicg(A,b,1e-6, maxit, L,U);
elapsed = toc;
disp(['BiCG: flag = ',num2str(flag),', iter = ',num2str(iter), ...
', exec time = ',num2str(elapsed)])
close all
figure
subplot(2,2,1)
semilogy(0:size(resvec,1)-1,resvec/norm(b),'-o')
title('BiCG')
xlabel('Iteration step')
ylabel('Relative residual')
tic
[x,flag,relres,iter,resvec] = qmr(A,b,1e-6, maxit,L,U);
elapsed = toc;
disp(['QMR: flag = ',num2str(flag),', iter = ',num2str(iter), ...
', exec time = ',num2str(elapsed)])
subplot(2,2,2)
semilogy(0:size(resvec,1)-1,resvec/norm(b),'-o')
title('QMR')
xlabel('Iteration step')
ylabel('Relative residual')
tic
[x,flag,relres,iter,resvec] = bicgstab(A,b,1e-6, maxit, L,U);
elapsed = toc;
disp(['BiCGstab: flag = ',num2str(flag),', iter = ',num2str(iter), ...
', exec time = ',num2str(elapsed)])
subplot(2,2,3)
semilogy(0:size(resvec,1)-1,resvec/norm(b),'-o')
title('BiCGstab')
xlabel('Iteration step')
ylabel('Relative residual')
tic
restart=20;
[x,flag,relres,iter,resvec] = gmres(A,b,restart,1e-6,maxit,L,U);
elapsed = toc;
disp(['GMRES: flag = ',num2str(flag), ...
', iter = ',num2str((iter(1)-1)*restart+iter(2)), ...
', exec time = ',num2str(elapsed)])
subplot(2,2,4)
semilogy(0:size(resvec,1)-1,resvec/norm(b),'-o')
title('GMRES(20)')
xlabel('Iteration step')
ylabel('Relative residual')