%==========================================================================
% DESCRIPTION : Demonstration of Rosenbrock test function and Himmelblau
% test function equations.  This code plots those functions over a range of
% interest using the Matlab MESHGRID and SURF functions. These functions
% are commonly used to test various function minimization algorithms
% because of their challenging shape (Rosenbrock) or multiple minima 
% (Himmelblau).
%
%
% AUTHOR : C. Alex Simpkins
%
% DATE : Wed. Nov. 17, 2007
%
% 
%==========================================================================


%---------------------------------------------------------------------
%------rosenbrock function--------------------------------------------

%over n points in either direction
n=100;

%the range of interest for each variable
xmin=-100;
xmax=100;
ymin=-10;
ymax=100;

%computing the vectors of each dimension
x=xmin:((xmax-xmin)/(n-1)):xmax;
y=ymin:((ymax-ymin)/(n-1)):ymax;

%computing the function of the variables (note we do this with one FOR loop
%instead of two (see vectorization help in matlab documentation)
for i=1:n,
    f(i,:) = (1-x).^2+100*(y(i)-x.^2).^2;
end

%meshgrid function is used to create the grid to plot over (so we have an
%XY pair for each Z 
[X,Y]=meshgrid(x,y);

%plot using the SURF command in matlab
surf(X,Y,f)

%interpolated shading for the plots...
shading interp

%label axes and title of the plot...
xlabel('x')
ylabel('y')
zlabel('f(x,y)')
title('Rosenbrock test function')

%create a new figure
figure

%---------------------------------------------------------------------
%--------himmelblau's function----------------------------------------

%over n points in either direction
n=100;

%the range of interest for each variable
xmin=-6;
xmax=6;
ymin=-6;
ymax=6;

%computing the vectors of each dimension
x=xmin:((xmax-xmin)/(n-1)):xmax;
y=ymin:((ymax-ymin)/(n-1)):ymax;

%computing the function of the variables (note we do this with one FOR loop
%instead of two (see vectorization help in matlab documentation)
for i=1:n,
    f(i,:) = (x.^2 + y(i) -11).^2 + (x + y(i).^2 - 7).^2;
end

%meshgrid function is used to create the grid to plot over (so we have an
%XY pair for each Z 
[X,Y]=meshgrid(x,y);


%plot using the SURF command in matlab
surf(X,Y,f)

%interpolated shading for the plots...
shading interp

%label axes and title of the plot...
xlabel('x')
ylabel('y')
zlabel('f(x,y)')
title('Himmelblau test function')