5837
|
1 ## Copyright (C) 2004 Piotr Krzyzanowski <piotr.krzyzanowski@mimuw.edu.pl> |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5838
|
21 ## @deftypefn {Function File} {@var{x} =} pcr (@var{a}, @var{b}, @var{tol}, @var{maxit}, @var{m}, @var{x0}, @dots{}) |
5837
|
22 ## @deftypefnx {Function File} {[@var{x}, @var{flag}, @var{relres}, @var{iter}, @var{resvec}] =} pcr (@dots{}) |
|
23 ## |
5838
|
24 ## Solves the linear system of equations @code{@var{a} * @var{x} = |
5837
|
25 ## @var{b}} by means of the Preconditioned Conjugate Residuals iterative |
|
26 ## method. The input arguments are |
|
27 ## |
|
28 ## @itemize |
|
29 ## @item |
5838
|
30 ## @var{a} can be either a square (preferably sparse) matrix or a |
5837
|
31 ## function handle, inline function or string containing the name |
5838
|
32 ## of a function which computes @code{@var{a} * @var{x}}. In principle |
|
33 ## @var{a} should be symmetric and non-singular; if @code{pcr} |
|
34 ## finds @var{a} to be numerically singular, you will get a warning |
5837
|
35 ## message and the @var{flag} output parameter will be set. |
|
36 ## |
|
37 ## @item |
|
38 ## @var{b} is the right hand side vector. |
|
39 ## |
|
40 ## @item |
|
41 ## @var{tol} is the required relative tolerance for the residual error, |
5838
|
42 ## @code{@var{b} - @var{a} * @var{x}}. The iteration stops if @code{norm |
|
43 ## (@var{b} - @var{a} * @var{x}) <= @var{tol} * norm (@var{b} - @var{a} * |
5837
|
44 ## @var{x0})}. If @var{tol} is empty or is omitted, the function sets |
|
45 ## @code{@var{tol} = 1e-6} by default. |
|
46 ## |
|
47 ## @item |
|
48 ## @var{maxit} is the maximum allowable number of iterations; if |
|
49 ## @code{[]} is supplied for @code{maxit}, or @code{pcr} has less |
|
50 ## arguments, a default value equal to 20 is used. |
|
51 ## |
|
52 ## @item |
5838
|
53 ## @var{m} is the (left) preconditioning matrix, so that the iteration is |
5837
|
54 ## (theoretically) equivalent to solving by @code{pcr} @code{@var{P} * |
5838
|
55 ## @var{x} = @var{m} \ @var{b}}, with @code{@var{P} = @var{m} \ @var{a}}. |
5837
|
56 ## Note that a proper choice of the preconditioner may dramatically |
|
57 ## improve the overall performance of the method. Instead of matrix |
5838
|
58 ## @var{m}, the user may pass a function which returns the results of |
|
59 ## applying the inverse of @var{m} to a vector (usually this is the |
5837
|
60 ## preferred way of using the preconditioner). If @code{[]} is supplied |
5838
|
61 ## for @var{m}, or @var{m} is omitted, no preconditioning is applied. |
5837
|
62 ## |
|
63 ## @item |
|
64 ## @var{x0} is the initial guess. If @var{x0} is empty or omitted, the |
|
65 ## function sets @var{x0} to a zero vector by default. |
|
66 ## @end itemize |
|
67 ## |
|
68 ## The arguments which follow @var{x0} are treated as parameters, and |
5838
|
69 ## passed in a proper way to any of the functions (@var{a} or @var{m}) |
5837
|
70 ## which are passed to @code{pcr}. See the examples below for further |
|
71 ## details. The output arguments are |
|
72 ## |
|
73 ## @itemize |
|
74 ## @item |
|
75 ## @var{x} is the computed approximation to the solution of |
5838
|
76 ## @code{@var{a} * @var{x} = @var{b}}. |
5837
|
77 ## |
|
78 ## @item |
|
79 ## @var{flag} reports on the convergence. @code{@var{flag} = 0} means |
|
80 ## the solution converged and the tolerance criterion given by @var{tol} |
|
81 ## is satisfied. @code{@var{flag} = 1} means that the @var{maxit} limit |
|
82 ## for the iteration count was reached. @code{@var{flag} = 3} reports t |
|
83 ## @code{pcr} breakdown, see [1] for details. |
|
84 ## |
|
85 ## @item |
|
86 ## @var{relres} is the ratio of the final residual to its initial value, |
|
87 ## measured in the Euclidean norm. |
|
88 ## |
|
89 ## @item |
|
90 ## @var{iter} is the actual number of iterations performed. |
|
91 ## |
|
92 ## @item |
|
93 ## @var{resvec} describes the convergence history of the method, |
|
94 ## so that @code{@var{resvec} (i)} contains the Euclidean norms of the |
|
95 ## residualafter the (@var{i}-1)-th iteration, @code{@var{i} = |
5838
|
96 ## 1,2, @dots{}, @var{iter}+1}. |
5837
|
97 ## @end itemize |
|
98 ## |
|
99 ## Let us consider a trivial problem with a diagonal matrix (we exploit the |
|
100 ## sparsity of A) |
|
101 ## |
|
102 ## @example |
|
103 ## @group |
|
104 ## N = 10; |
|
105 ## A = diag([1:N]); A = sparse(A); |
|
106 ## b = rand(N,1); |
|
107 ## @end group |
|
108 ## @end example |
|
109 ## |
|
110 ## @sc{Example 1:} Simplest use of @code{pcr} |
|
111 ## |
|
112 ## @example |
|
113 ## x = pcr(A, b) |
|
114 ## @end example |
|
115 ## |
|
116 ## @sc{Example 2:} @code{pcr} with a function which computes |
5838
|
117 ## @code{@var{a} * @var{x}}. |
5837
|
118 ## |
|
119 ## @example |
|
120 ## @group |
|
121 ## function y = applyA(x) |
|
122 ## y = [1:10]'.*x; |
|
123 ## endfunction |
|
124 ## |
|
125 ## x = pcr('applyA',b) |
|
126 ## @end group |
|
127 ## @end example |
|
128 ## |
|
129 ## @sc{Example 3:} Preconditioned iteration, with full diagnostics. The |
|
130 ## preconditioner (quite strange, because even the original matrix |
5838
|
131 ## @var{a} is trivial) is defined as a function |
5837
|
132 ## |
|
133 ## @example |
|
134 ## @group |
|
135 ## function y = applyM(x) |
|
136 ## K = floor(length(x)-2); |
|
137 ## y = x; |
|
138 ## y(1:K) = x(1:K)./[1:K]'; |
|
139 ## endfunction |
|
140 ## |
|
141 ## [x, flag, relres, iter, resvec] = pcr(A,b,[],[],'applyM') |
|
142 ## semilogy([1:iter+1], resvec); |
|
143 ## @end group |
|
144 ## @end example |
|
145 ## |
|
146 ## @sc{Example 4:} Finally, a preconditioner which depends on a |
|
147 ## parameter @var{k}. |
|
148 ## |
|
149 ## @example |
|
150 ## @group |
|
151 ## function y = applyM(x, varargin) |
|
152 ## K = varargin@{1@}; |
|
153 ## y = x; y(1:K) = x(1:K)./[1:K]'; |
|
154 ## endfunction |
|
155 ## |
|
156 ## [x, flag, relres, iter, resvec] = pcr(A,b,[],[],'applyM',[],3) |
|
157 ## @end group |
|
158 ## @end example |
|
159 ## |
|
160 ## @sc{References} |
|
161 ## |
|
162 ## [1] W. Hackbusch, "Iterative Solution of Large Sparse Systems of |
|
163 ## Equations", section 9.5.4; Springer, 1994 |
|
164 ## |
|
165 ## @seealso{sparse, pcg} |
|
166 ## @end deftypefn |
|
167 |
5838
|
168 ## Author: Piotr Krzyzanowski <piotr.krzyzanowski@mimuw.edu.pl> |
5837
|
169 |
5838
|
170 function [x, flag, relres, iter, resvec] = pcr (A, b, tol, maxit, M, x0, varargin) |
5837
|
171 |
|
172 breakdown = false; |
|
173 |
5838
|
174 if (nargin < 6 || isempty (x0)) |
|
175 x = zeros (size (b)); |
5837
|
176 else |
|
177 x = x0; |
|
178 endif |
|
179 |
|
180 if (nargin < 5) |
|
181 M = []; |
|
182 endif |
|
183 |
5838
|
184 if (nargin < 4 || isempty (maxit)) |
5837
|
185 maxit = 20; |
|
186 endif |
|
187 |
5838
|
188 maxit += 2; |
5837
|
189 |
5838
|
190 if (nargin < 3 || isempty (tol)) |
5837
|
191 tol = 1e-6; |
|
192 endif |
|
193 |
|
194 if (nargin < 2) |
5838
|
195 print_usage (); |
5837
|
196 endif |
|
197 |
|
198 ## init |
5838
|
199 if (isnumeric (A)) # is A a matrix? |
|
200 r = b - A*x; |
5837
|
201 else # then A should be a function! |
5838
|
202 r = b - feval (A, x, varargin{:}); |
5837
|
203 endif |
|
204 |
5838
|
205 if (isnumeric (M)) # is M a matrix? |
|
206 if (isempty (M)) # if M is empty, use no precond |
5837
|
207 p = r; |
|
208 else # otherwise, apply the precond |
5838
|
209 p = M \ r; |
5837
|
210 endif |
|
211 else # then M should be a function! |
5838
|
212 p = feval (M, r, varargin{:}); |
5837
|
213 endif |
|
214 |
|
215 iter = 2; |
|
216 |
|
217 b_bot_old = 1; |
5838
|
218 q_old = p_old = s_old = zeros (size (x)); |
5837
|
219 |
5838
|
220 if (isnumeric (A)) # is A a matrix? |
|
221 q = A * p; |
5837
|
222 else # then A should be a function! |
5838
|
223 q = feval (A, p, varargin{:}); |
5837
|
224 endif |
|
225 |
5838
|
226 resvec(1) = abs (norm (r)); |
5837
|
227 |
|
228 ## iteration |
5838
|
229 while (resvec(iter-1) > tol*resvec(1) && iter < maxit) |
|
230 |
|
231 if (isnumeric (M)) # is M a matrix? |
|
232 if (isempty (M)) # if M is empty, use no precond |
5837
|
233 s = q; |
|
234 else # otherwise, apply the precond |
5838
|
235 s = M \ q; |
5837
|
236 endif |
|
237 else # then M should be a function! |
5838
|
238 s = feval (M, q, varargin{:}); |
5837
|
239 endif |
5838
|
240 b_top = r' * s; |
|
241 b_bot = q' * s; |
5837
|
242 |
|
243 if (b_bot == 0.0) |
|
244 breakdown = true; |
|
245 break; |
|
246 endif |
5838
|
247 lambda = b_top / b_bot; |
5837
|
248 |
5838
|
249 x += lambda*p; |
|
250 r -= lambda*q; |
5837
|
251 |
|
252 if (isnumeric(A)) # is A a matrix? |
|
253 t = A*s; |
|
254 else # then A should be a function! |
5838
|
255 t = feval (A, s, varargin{:}); |
5837
|
256 endif |
|
257 |
5838
|
258 alpha0 = (t'*s) / b_bot; |
|
259 alpha1 = (t'*s_old) / b_bot_old; |
5837
|
260 |
5838
|
261 p_temp = p; |
|
262 q_temp = q; |
|
263 |
5837
|
264 p = s - alpha0*p - alpha1*p_old; |
|
265 q = t - alpha0*q - alpha1*q_old; |
|
266 |
|
267 s_old = s; |
|
268 p_old = p_temp; |
|
269 q_old = q_temp; |
|
270 b_bot_old = b_bot; |
|
271 |
5838
|
272 resvec(iter) = abs (norm (r)); |
|
273 iter++; |
5837
|
274 endwhile |
|
275 |
|
276 flag = 0; |
5838
|
277 relres = resvec(iter-1) ./ resvec(1); |
|
278 iter -= 2; |
|
279 if (iter >= maxit-2) |
5837
|
280 flag = 1; |
|
281 if (nargout < 2) |
6498
|
282 warning ("pcr: maximum number of iterations (%d) reached\n", iter); |
|
283 warning ("the initial residual norm was reduced %g times.\n", 1.0/relres); |
5837
|
284 endif |
5838
|
285 elseif (nargout < 2 && ! breakdown) |
6498
|
286 fprintf (stderr, "pcr: converged in %d iterations. \n", iter); |
|
287 fprintf (stderr, "the initial residual norm was reduced %g times.\n", |
5838
|
288 1.0/relres); |
5837
|
289 endif |
5838
|
290 |
5837
|
291 if (breakdown) |
|
292 flag = 3; |
|
293 if (nargout < 2) |
6498
|
294 warning ("pcr: breakdown occured:\n"); |
|
295 warning ("system matrix singular or preconditioner indefinite?\n"); |
5837
|
296 endif |
|
297 endif |
|
298 |
|
299 endfunction |
|
300 |
|
301 %!demo |
|
302 %! |
|
303 %! # Simplest usage of PCR (see also 'help pcr') |
|
304 %! |
|
305 %! N = 20; |
|
306 %! A = diag(linspace(-3.1,3,N)); b = rand(N,1); y = A\b; #y is the true solution |
|
307 %! x = pcr(A,b); |
|
308 %! printf('The solution relative error is %g\n', norm(x-y)/norm(y)); |
|
309 %! |
|
310 %! # You shouldn't be afraid if PCR issues some warning messages in this |
|
311 %! # example: watch out in the second example, why it takes N iterations |
|
312 %! # of PCR to converge to (a very accurate, by the way) solution |
|
313 %!demo |
|
314 %! |
|
315 %! # Full output from PCR |
|
316 %! # We use this output to plot the convergence history |
|
317 %! |
|
318 %! N = 20; |
|
319 %! A = diag(linspace(-3.1,30,N)); b = rand(N,1); X = A\b; #X is the true solution |
|
320 %! [x, flag, relres, iter, resvec] = pcr(A,b); |
|
321 %! printf('The solution relative error is %g\n', norm(x-X)/norm(X)); |
|
322 %! title('Convergence history'); xlabel('Iteration'); ylabel('log(||b-Ax||/||b||)'); |
|
323 %! semilogy([0:iter],resvec/resvec(1),'o-g;relative residual;'); |
|
324 %!demo |
|
325 %! |
|
326 %! # Full output from PCR |
|
327 %! # We use indefinite matrix based on the Hilbert matrix, with one |
|
328 %! # strongly negative eigenvalue |
|
329 %! # Hilbert matrix is extremely ill conditioned, so is ours, |
|
330 %! # and that's why PCR WILL have problems |
|
331 %! |
|
332 %! N = 10; |
|
333 %! A = hilb(N); A(1,1)=-A(1,1); b = rand(N,1); X = A\b; #X is the true solution |
|
334 %! printf('Condition number of A is %g\n', cond(A)); |
|
335 %! [x, flag, relres, iter, resvec] = pcr(A,b,[],200); |
|
336 %! if (flag == 3) |
|
337 %! printf('PCR breakdown. System matrix is [close to] singular\n'); |
|
338 %! end |
|
339 %! title('Convergence history'); xlabel('Iteration'); ylabel('log(||b-Ax||)'); |
|
340 %! semilogy([0:iter],resvec,'o-g;absolute residual;'); |
|
341 %!demo |
|
342 %! |
|
343 %! # Full output from PCR |
|
344 %! # We use an indefinite matrix based on the 1-D Laplacian matrix for A, |
|
345 %! # and here we have cond(A) = O(N^2) |
|
346 %! # That's the reason we need some preconditioner; here we take |
|
347 %! # a very simple and not powerful Jacobi preconditioner, |
|
348 %! # which is the diagonal of A |
|
349 %! |
|
350 %! # Note that we use here indefinite preconditioners! |
|
351 %! |
|
352 %! N = 100; |
|
353 %! A = zeros(N,N); |
|
354 %! for i=1:N-1 # form 1-D Laplacian matrix |
|
355 %! A(i:i+1,i:i+1) = [2 -1; -1 2]; |
|
356 %! endfor |
|
357 %! A = [A, zeros(size(A)); zeros(size(A)), -A]; |
|
358 %! b = rand(2*N,1); X = A\b; #X is the true solution |
|
359 %! maxit = 80; |
|
360 %! printf('System condition number is %g\n',cond(A)); |
|
361 %! # No preconditioner: the convergence is very slow! |
|
362 %! |
|
363 %! [x, flag, relres, iter, resvec] = pcr(A,b,[],maxit); |
|
364 %! title('Convergence history'); xlabel('Iteration'); ylabel('log(||b-Ax||)'); |
|
365 %! semilogy([0:iter],resvec,'o-g;NO preconditioning: absolute residual;'); |
|
366 %! |
|
367 %! pause(1); |
|
368 %! # Test Jacobi preconditioner: it will not help much!!! |
|
369 %! |
|
370 %! M = diag(diag(A)); # Jacobi preconditioner |
|
371 %! [x, flag, relres, iter, resvec] = pcr(A,b,[],maxit,M); |
|
372 %! hold on; |
|
373 %! semilogy([0:iter],resvec,'o-r;JACOBI preconditioner: absolute residual;'); |
|
374 %! |
|
375 %! pause(1); |
|
376 %! # Test nonoverlapping block Jacobi preconditioner: this one should give |
|
377 %! # some convergence speedup! |
|
378 %! |
|
379 %! M = zeros(N,N);k=4; |
|
380 %! for i=1:k:N # get k x k diagonal blocks of A |
|
381 %! M(i:i+k-1,i:i+k-1) = A(i:i+k-1,i:i+k-1); |
|
382 %! endfor |
|
383 %! M = [M, zeros(size(M)); zeros(size(M)), -M]; |
|
384 %! [x, flag, relres, iter, resvec] = pcr(A,b,[],maxit,M); |
|
385 %! semilogy([0:iter],resvec,'o-b;BLOCK JACOBI preconditioner: absolute residual;'); |
|
386 %! hold off; |
|
387 %!test |
|
388 %! |
|
389 %! #solve small indefinite diagonal system |
|
390 %! |
|
391 %! N = 10; |
|
392 %! A = diag(linspace(-10.1,10,N)); b = ones(N,1); X = A\b; #X is the true solution |
|
393 %! [x, flag] = pcr(A,b,[],N+1); |
|
394 %! assert(norm(x-X)/norm(X)<1e-10); |
|
395 %! assert(flag,0); |
|
396 %! |
|
397 %!test |
|
398 %! |
|
399 %! #solve tridiagonal system, do not converge in default 20 iterations |
|
400 %! #should perform max allowable default number of iterations |
|
401 %! |
|
402 %! N = 100; |
|
403 %! A = zeros(N,N); |
|
404 %! for i=1:N-1 # form 1-D Laplacian matrix |
|
405 %! A(i:i+1,i:i+1) = [2 -1; -1 2]; |
|
406 %! endfor |
|
407 %! b = ones(N,1); X = A\b; #X is the true solution |
|
408 %! [x, flag, relres, iter, resvec] = pcr(A,b,1e-12); |
|
409 %! assert(flag,1); |
|
410 %! assert(relres>0.6); |
|
411 %! assert(iter,20); |
|
412 %! |
|
413 %!test |
|
414 %! |
|
415 %! #solve tridiagonal system with 'prefect' preconditioner |
|
416 %! #converges in one iteration |
|
417 %! |
|
418 %! N = 100; |
|
419 %! A = zeros(N,N); |
|
420 %! for i=1:N-1 # form 1-D Laplacian matrix |
|
421 %! A(i:i+1,i:i+1) = [2 -1; -1 2]; |
|
422 %! endfor |
|
423 %! b = ones(N,1); X = A\b; #X is the true solution |
|
424 %! [x, flag, relres, iter] = pcr(A,b,[],[],A,b); |
|
425 %! assert(norm(x-X)/norm(X)<1e-6); |
|
426 %! assert(relres<1e-6); |
|
427 %! assert(flag,0); |
|
428 %! assert(iter,1); #should converge in one iteration |
|
429 %! |