Modify Example 32.1.1 so that it minimizes the simplified Rosenbrock’s function
\begin{equation}
f(\mathbf x) = (x_1 - 1)^2 + (x_1^2 - x_2)^2\tag{32.3.1}
\end{equation}
using conjugate gradient method. Use a random starting point
randn(2, 1). Plot the path of the search.Solution.
f = @(x) (x(1)-1).^2 + (x(1).^2 - x(2)).^2;
fg = @(x) [2*(x(1)-1) + 4*x(1)*(x(1).^2 - x(2)); -2*(x(1).^2 - x(2))];
x = randn(2, 1);
v = zeros(2, 1);
gamma = 0;
max_tries = 10000;
path = zeros(2, max_tries);
for k = 1:max_tries
path(:, k) = x;
w = -fg(x) + gamma*v; % with correction
t_min = fminbnd(@(t) f(x + t*w), 0, 1);
dx = t_min*w;
if norm(dx) < 1e-6
break
end
gamma = norm(fg(x+dx))^2/norm(fg(x))^2; % update gamma
x = x + dx; % update x
v = w; % record the step for the future
end
plot(path(1, 1:k), path(2, 1:k), '-+')
if k < max_tries
fprintf('Found x = (%g, %g) with f(x) = %g after %d steps\n', x, f(x), k);
else
disp('Failed to converge')
end
Compare the search path to what we get without correction in
w: it is less zigzagging. However, if we use the original Rosenbrock function (32.1.2) which is very far from quadratic or convex, the search diverges to infinity. Several other correction terms (different choices for \(\gamma\) coefficient) have been proposed for the nonlinear conjugate gradient method. But it appears that the “narrow valley” landscape of the Rosenbrock function is best navigated either by using both first and second order derivatives (the Newton method) or by using no derivatives at all (the Nelder-Mead method, to be considered later).
