% Plots Rosenbrock's function.
% Min is at (1,1), where f(x)=0.

x = -2:0.05:2;
y = -1:0.05:3;
[X, Y] = meshgrid(x, y);

Z = 100.*(Y-X.^2).^2 + (1-X).^2;

% Plot surface with a vertical offset of 2000:
surf(X, Y, Z+2000)
colormap(hsv(128*128))
shading interp
set(gca, 'zticklabel', '')
rblim = get(gca, 'clim');
title('Rosenbrock''s Function')
hold on

% Plot 20 contours with equal vertical spacing:
contour(X, Y, Z+2000, 20)

% Plot minimum:
plot3(1, 1, 0, 'ok', 'LineWidth', 2, 'MarkerSize', 5, 'MarkerFaceColor', 'w')
plot3(1, 1, 2000, 'ok', 'LineWidth', 2, 'MarkerSize', 5, 'MarkerFaceColor', 'w')
t=0:.05:1;
z = 2000.*t;
plot3(1, 1, z, 'ok', 'MarkerSize', 2, 'MarkerFaceColor', 'w')

% Format
axis tight
axis square

clear x y z X Y Z t