X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=optimization%2Fvanilla_cgm.m;h=b511b484dfaab2ecba7fa6b8702be5080f977ba9;hp=9ada2cb8026db12d5f66ac518879b2a3b438b398;hb=e12c489da09259a1365ae199fb6d7cbe64c9eb19;hpb=8df0aa3b8e47b596626cf0f833d9e0c0143bf4d5 diff --git a/optimization/vanilla_cgm.m b/optimization/vanilla_cgm.m index 9ada2cb..b511b48 100644 --- a/optimization/vanilla_cgm.m +++ b/optimization/vanilla_cgm.m @@ -40,18 +40,12 @@ function [x, k] = vanilla_cgm(A, b, x0, tolerance, max_iterations) sqrt_n = floor(sqrt(length(x0))); k = 0; - xk = x0; % Eschew the 'k' suffix on 'x' for simplicity. + xk = x0; rk = A*xk - b; % The first residual must be computed the hard way. pk = -rk; - while (k <= max_iterations) - if (norm(rk) < tolerance) - % Success. - x = xk; - return; - end - - alpha_k = step_length_cgm(rk, A, pk); + while (k <= max_iterations && norm(rk, 'inf') > tolerance) + alpha_k = step_length_positive_definite(rk, A, pk); x_next = xk + alpha_k*pk; % Avoid accumulated roundoff errors.