]> gitweb.michael.orlitzky.com - octave.git/blobdiff - optimization/conjugate_gradient_method.m
Move conjugate_gradient_method.m to vanilla_cgm.m.
[octave.git] / optimization / conjugate_gradient_method.m
index 2c94487a65508e876903343abf457cda853bb929..a6401e5ec21ff963883368576deb59ff0e1b36d0 100644 (file)
@@ -8,8 +8,7 @@ function [x, k] = conjugate_gradient_method(A, b, x0, tolerance, max_iterations)
   %
   %   min [phi(x) = (1/2)*<Ax,x> + <b,x>]
   %
   %
   %   min [phi(x) = (1/2)*<Ax,x> + <b,x>]
   %
-  % using the conjugate_gradient_method (Algorithm 5.2 in Nocedal and
-  % Wright).
+  % using the conjugate_gradient_method.
   %
   % INPUT:
   %
   %
   % INPUT:
   %
@@ -36,28 +35,15 @@ function [x, k] = conjugate_gradient_method(A, b, x0, tolerance, max_iterations)
   %
   % All vectors are assumed to be *column* vectors.
   %
   %
   % All vectors are assumed to be *column* vectors.
   %
-  zero_vector = zeros(length(x0), 1);
+  n = length(x0);
+  M = eye(n);
 
 
-  k = 0;
-  x = x0; % Eschew the 'k' suffix on 'x' for simplicity.
-  rk = A*x - b; % The first residual must be computed the hard way.
-  pk = -rk;
-
-  for k = [ 0 : max_iterations ]
-    if (norm(rk) < tolerance)
-       % Success.
-       return;
-    end
-
-    alpha_k = step_length_cgm(rk, A, pk);
-    x_next = x + alpha_k*pk;
-    r_next = rk + alpha_k*A*pk;
-    beta_next = (r_next' * r_next)/(rk' * rk);
-    p_next = -r_next + beta_next*pk;
-
-    k = k + 1;
-    x = x_next;
-    rk = r_next;
-    pk = p_next;
-  end
+  % The standard CGM is equivalent to the preconditioned CGM is you
+  % use the identity matrix as your preconditioner.
+  [x, k] = preconditioned_conjugate_gradient_method(A,
+                                                   M,
+                                                   b,
+                                                   x0,
+                                                   tolerance,
+                                                   max_iterations);
 end
 end