]> gitweb.michael.orlitzky.com - octave.git/blobdiff - optimization/preconditioned_conjugate_gradient_method.m
Fix the iteration count in the PCGM.
[octave.git] / optimization / preconditioned_conjugate_gradient_method.m
index 4e68ccb583e7fab2bc8cf9f34ce170eeab7fe3a2..35fecaa99cdb81b7f22836791797c0bb2fe1acac 100644 (file)
@@ -77,7 +77,7 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, ...
   zk = M \ rk;
   dk = -zk;
 
   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.
 
     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;
 
     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;
     k = k + 1;
     xk = x_next;
     rk = r_next;