comparison toolboxes/alps/ALPS/cgsolve.m @ 154:0de08f68256b ivand_dev

ALPS toolbox - Algebraic Pursuit added to smallbox
author Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk>
date Fri, 12 Aug 2011 11:17:47 +0100
parents
children
comparison
equal deleted inserted replaced
153:af307f247ac7 154:0de08f68256b
1 % cgsolve.m
2 %
3 % Solve a symmetric positive definite system Ax = b via conjugate gradients.
4 %
5 % Usage: [x, res, iter] = cgsolve(A, b, tol, maxiter, verbose)
6 %
7 % A - Either an NxN matrix, or a function handle.
8 %
9 % b - N vector
10 %
11 % tol - Desired precision. Algorithm terminates when
12 % norm(Ax-b)/norm(b) < tol .
13 %
14 % maxiter - Maximum number of iterations.
15 %
16 % verbose - If 0, do not print out progress messages.
17 % Default = 1.
18 %
19 % Written by: Justin Romberg, Caltech
20 % Email: jrom@acm.caltech.edu
21 % Created: October 2005
22 %
23
24 function [x, res, iter] = cgsolve(A, b, tol, maxiter, verbose)
25
26 if (nargin < 5), verbose = 1; end
27
28 implicit = isa(A,'function_handle');
29
30 x = zeros(length(b),1);
31 r = b;
32 d = r;
33 delta = r'*r;
34 delta0 = b'*b;
35 numiter = 0;
36 bestx = x;
37 bestres = sqrt(delta/delta0);
38 while ((numiter < maxiter) & (delta > tol^2*delta0))
39
40 % q = A*d
41 if (implicit), q = A(d); else, q = A*d; end
42
43 alpha = delta/(d'*q);
44 x = x + alpha*d;
45
46 if (mod(numiter+1,50) == 0)
47 % r = b - Aux*x
48 if (implicit), r = b - A(x); else, r = b - A*x; end
49 else
50 r = r - alpha*q;
51 end
52
53 deltaold = delta;
54 delta = r'*r;
55 beta = delta/deltaold;
56 d = r + beta*d;
57 numiter = numiter + 1;
58 if (sqrt(delta/delta0) < bestres)
59 bestx = x;
60 bestres = sqrt(delta/delta0);
61 end
62
63 if ((verbose) & (mod(numiter,50)==0))
64 disp(sprintf('cg: Iter = %d, Best residual = %8.3e, Current residual = %8.3e', ...
65 numiter, bestres, sqrt(delta/delta0)));
66 end
67
68 end
69
70 if (verbose)
71 disp(sprintf('cg: Iterations = %d, best residual = %14.8e', numiter, bestres));
72 end
73 x = bestx;
74 res = bestres;
75 iter = numiter;
76