% Nelder-Mead Demo using fminsearch.

%************ OPTIONS ***************

% Set the max number of function evaluations.
options = optimset('MaxFunEvals', 211);

% Turn on the display of iterative output:
options = optimset(options, 'Display', 'iter'); 

%********* END OPTIONS **********

% Plot the objective (Rosenbrock's function):
rbplot
title('Function Evaluations')

% Set the initial value and run the optimization:
x0 = [-1.9, 2];
global iterations
iterations = x0;
[x,fval,exitflag,output] = ...
    fminsearch(@rbfun, x0, options)

% Plot the iterations.
plot(iterations(:,1), iterations(:,2), ...
      'ko', 'MarkerSize', 3, 'MarkerFaceColor', 'r')
for i = 1:length(iterations)
plot3(iterations(i,1), iterations(i,2), ...
        rbfun([iterations(i,1), iterations(i,2)]) + 2000, ...
        'ko', 'MarkerSize', 3, 'MarkerFaceColor', 'w')
end

clear i rblim iterations