% min [phi(x) = (1/2)*<Qx,x> + <b,x>]
%
% using the preconditioned conjugate gradient method (14.56 in
- % Guler). If ``M`` is the identity matrix, we use the slightly
- % faster implementation in conjugate_gradient_method.m.
+ % Guler).
%
% INPUT:
%
%
% We use this in the inner loop.
- sqrt_n = floor(sqrt(length(x0)));
+ n = length(x0);
+ sqrt_n = floor(sqrt(n));
% Set k=0 first, that way the references to xk,rk,zk,dk which
% immediately follow correspond (semantically) to x0,r0,z0,d0.
zk = M \ rk;
dk = -zk;
- for k = [ 0 : max_iterations ]
-
- if (norm(rk) < tolerance)
- % Check our stopping condition. This should catch the k=0 case.
- x = xk;
- return;
- end
-
+ while (k <= max_iterations && norm(rk, 'inf') > tolerance)
% Used twice, avoid recomputation.
rkzk = rk' * zk;
% do them both, so we precompute the more expensive operation.
Qdk = Q * dk;
- alpha_k = rkzk/(dk' * Qdk);
+ % We're going to divide by this quantity...
+ dkQdk = dk' * Qdk;
+
+ % So if it's too close to zero, we replace it with something
+ % comparable but non-zero.
+ if (dkQdk < eps)
+ dkQdk = eps;
+ end
+
+ alpha_k = rkzk/dkQdk;
x_next = xk + (alpha_k * dk);
% The recursive definition of r_next is prone to accumulate
% roundoff error. When sqrt(n) divides k, we recompute the
- % residual to minimize this error. This modification is due to the
- % second reference.
+ % residual to minimize this error. This modification was suggested
+ % by the second reference.
if (mod(k, sqrt_n) == 0)
r_next = Q*x_next - b;
else
beta_next = (r_next' * z_next)/rkzk;
d_next = -z_next + beta_next*dk;
+ % We potentially just performed one more iteration than necessary
+ % in order to simplify the loop. Note that due to the structure of
+ % our loop, we will have k > max_iterations when we fail to
+ % converge.
k = k + 1;
xk = x_next;
rk = r_next;
dk = d_next;
end
- % The algorithm didn't converge, but we still want to return the
- % terminal value of xk.
+ % If we make it here, one of the two stopping conditions was met.
x = xk;
end