function testfcn ( iexmpl );
% TestFcn
%
% This m-file is part of the material of the lectures on
%   Numerical Methods in Nonlinear Optimization
% at the Math Department of Halle University, Germany.
%
% Author :      Martin Arnold, martin.arnold@mathematik.uni-halle.de
% Version of :  Apr 11, 2006
%
% Visualization of Test Functions in nonlinear optimization
%
% Parameter:
%   iexmpl (input)  : control flag "example"
%                       =1 .. test function with many local minima
%                       =2 .. quadratic test function
%                       =3 .. Function of Rosenbrock
%                       =4 .. Function of Himmelblau
%                       =5 .. Function of Bazaraa / Shetty
%
% Examples:
%   testfcn ( 1 );
%   testfcn ( 2 );
%   testfcn ( 3 );
%   testfcn ( 4 );
%   testfcn ( 5 );
%
% Reference: 
%   W. Alt: Nichtlineare Optimierung. - Vieweg Braunschweig. - 2002.  

% -> initialization
Fmax = NaN;
Vvec = NaN;
carg = NaN;

% -> problem definition
if iexmpl==1,
    F = @(X1,X2) ( ( X1.^2 + X2.^2 ) / 2 - cos ( X1.^2 ) - cos ( X2.^2 ) );
    x1min = -5.0;
    x1max =  5.0;
    x2min = -5.0;
    x2max =  5.0;
    tstr  = 'Zielfunktion mit mehreren lokalen Minima, vgl. Alt (2002), Beispiel 1.1.2';
    carg  =  20;
elseif iexmpl==2,
    F = @(X1,X2) ( 2*X1.^2 + X2.^2 - 4*X1 - 2*X2 + 3 );
    x1min = -10.0;
    x1max =  10.0;
    x2min = -10.0;
    x2max =  10.0;
    tstr  = 'Quadratische Zielfunktion, vgl. Alt (2002), Beispiel 1.2.5';
elseif iexmpl==3,
    F = @(X1,X2) ( 100 * ( X2 - X1.^2 ).^2 + ( 1 - X1 ).^2 );
    x1min = -2.5;
    x1max =  2.0;
    x2min = -0.5;
    x2max =  3.5;
    tstr  = 'Funktion von Rosenbrock, vgl. Alt (2002), Beispiel 1.4.1';
    Fmax  = 2000;
    Vvec  = [ 135 30 ];
    carg  = logspace ( 0, 3, 10 );
elseif iexmpl==4,
    F = @(X1,X2) ( ( X1.^2 + X2 - 11 ).^2 + ( X1 + X2.^2 - 7 ).^2 );
    x1min = -5.0;
    x1max =  5.0;
    x2min = -5.0;
    x2max =  5.0;
    tstr  = 'Funktion von Himmelblau, vgl. Alt (2002), Beispiel 1.4.2';
    carg  =  [ 0:10:200, 220, 240:40:800 ];
elseif iexmpl==5,
    F = @(X1,X2) ( ( X1 - 2 ).^4 + ( X1 - 2*X2 ).^2 );
    x1min =  1.6;
    x1max =  2.6;
    x2min =  0.8;    
    x2max =  1.3;
    tstr  = 'Funktion von Bazaraa / Shetty, vgl. Alt (2002), Beispiel 1.4.3';
    Fmax  =  1.0;
    carg  =  [ 0.01, 0.033, 0.1, 0.2:0.2:1.0 ];
end;

% -> definition of output grid etc.
nx = 161;
x1 = linspace ( x1min, x1max, nx );
x2 = linspace ( x2min, x2max, nx );
[ X1, X2 ] = meshgrid ( x1, x2 ); 

Fdat = F(X1,X2);

% -> graphical output: mesh plot
figure ( 1 );

Fmesh = Fdat;
if ~isnan(Fmax),
    Fmesh(Fmesh>Fmax) = NaN;
end;

mesh ( X1, X2, Fmesh );
xlabel ( 'x_1' );
ylabel ( 'x_2' );
zlabel ( 'f(x_1,x_2)' );
title ( tstr );

if min(isnan(Vvec))==0,
    view ( Vvec );
end;

% -> graphical output: contour plot
figure ( 2 );

if min(isnan(carg))==0,
    [ c, h ] = contour ( X1, X2, Fdat, carg );
else
    [ c, h ] = contour ( X1, X2, Fdat );
end;

if ( (iexmpl==2) | (iexmpl==5) ),
    clabel ( c, h );
end;

xlabel ( 'x_1' );
ylabel ( 'x_2' );
title ( tstr );
