%% An example of the Nelder-Mead procedure

%% Use the Himmelblau function
p = problem2d('himmel');
region = p.region;
x0 = [0; 0.6];
% Show the contour plot
clf;
h = ezcontour(p.fonly,region');
set(h,'LevelList',logspace(-2,4,25));

%% Show the steps of the Nelder-Mead procedure
X = 2.5*randn(2,3);
for i=1:25
    clf;
    h = ezcontour(p.fonly,region');
    Xnew = nelder_mead_step(p.f,X);
    set(h,'LevelList',logspace(-2,4,25));
    hold on;
    patch(X(1,:), X(2,:),'r','LineWidth',2,'FaceColor','none');
    patch(Xnew(1,:), Xnew(2,:),'r','EdgeColor','r','LineWidth',2,'FaceColor','none');
    hold off;
    pause(2);
    X = Xnew;
end
