From 302a62bb7c0adf581916fcd1f93faa719e8d51e8 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 22 Mar 2013 15:48:12 -0400 Subject: [PATCH] Fix the iteration count in the PCGM. --- optimization/preconditioned_conjugate_gradient_method.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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; -- 2.44.2