From: Michael Orlitzky Date: Fri, 22 Mar 2013 19:48:12 +0000 (-0400) Subject: Fix the iteration count in the PCGM. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=302a62bb7c0adf581916fcd1f93faa719e8d51e8;p=octave.git Fix the iteration count in the PCGM. --- diff --git a/optimization/preconditioned_conjugate_gradient_method.m b/optimization/preconditioned_conjugate_gradient_method.m index 4e68ccb..35fecaa 100644 --- a/optimization/preconditioned_conjugate_gradient_method.m +++ b/optimization/preconditioned_conjugate_gradient_method.m @@ -77,7 +77,7 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, ... zk = M \ rk; dk = -zk; - for k = [ 0 : max_iterations ] + while (k <= max_iterations) if (norm(rk) < tolerance) % Check our stopping condition. This should catch the k=0 case. @@ -109,6 +109,10 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, ... 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;