X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=optimization%2Fconjugate_gradient_method.m;h=a6401e5ec21ff963883368576deb59ff0e1b36d0;hp=9b2ddac24377e5d02651f7b9e52a5ee3a4c0707c;hb=5ef96eb244b2a52886a31fc9873db479f52a6483;hpb=6e504c235e5e2ad1abc0a98b733ed2269936c17a diff --git a/optimization/conjugate_gradient_method.m b/optimization/conjugate_gradient_method.m index 9b2ddac..a6401e5 100644 --- a/optimization/conjugate_gradient_method.m +++ b/optimization/conjugate_gradient_method.m @@ -1,4 +1,4 @@ -function x_star = conjugate_gradient_method(A, b, x0, tolerance) +function [x, k] = conjugate_gradient_method(A, b, x0, tolerance, max_iterations) % % Solve, % @@ -8,7 +8,7 @@ function x_star = conjugate_gradient_method(A, b, x0, tolerance) % % min [phi(x) = (1/2)* + ] % - % using Algorithm 5.2 in Nocedal and Wright. + % using the conjugate_gradient_method. % % INPUT: % @@ -22,33 +22,28 @@ function x_star = conjugate_gradient_method(A, b, x0, tolerance) % - ``tolerance`` -- How close ``Ax`` has to be to ``b`` (in % magnitude) before we stop. % + % - ``max_iterations`` -- The maximum number of iterations to perform. + % % OUTPUT: % - % - ``x_star`` - The solution to Ax=b. + % - ``x`` - The solution to Ax=b. + % + % - ``k`` - The ending value of k; that is, the number of iterations that + % were performed. % % NOTES: % % All vectors are assumed to be *column* vectors. % - zero_vector = zeros(length(x0), 1); - - k = 0; - xk = x0; - rk = A*xk - b; % The first residual must be computed the hard way. - pk = -rk; - - while (norm(rk) > tolerance) - alpha_k = step_length_cgm(rk, A, pk); - x_next = xk + 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; - xk = x_next; - rk = r_next; - pk = p_next; - end + n = length(x0); + M = eye(n); - x_star = xk; + % 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