+ % Set k=0 first, that way the references to xk,rk,zk,dk which
+ % immediately follow correspond (semantically) to x0,r0,z0,d0.
+ k = 0;
+
+ xk = x0;
+ rk = Q*xk - b;
+ 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
+
+ % Used twice, avoid recomputation.
+ rkzk = rk' * zk;
+
+ % The term alpha_k*dk appears twice, but so does Q*dk. We can't
+ % do them both, so we precompute the more expensive operation.
+ Qdk = Q * dk;
+
+ % After substituting the two previously-created variables, the
+ % following algorithm occurs verbatim in the reference.
+ alpha_k = rkzk/(dk' * Qdk);
+ x_next = xk + (alpha_k * dk);
+ r_next = rk + (alpha_k * Qdk);
+ z_next = M \ r_next;
+ beta_next = (r_next' * z_next)/rkzk;
+ d_next = -z_next + beta_next*dk;