echo off
%TUTDEMO Tutorial for Optimization Toolbox.

%   Copyright (c) 1990-98 by The MathWorks, Inc.
%   $Revision: 1.15 $  $Date: 1998/09/10 16:02:01 $

echo on; clc
%TUTDEMO Tutorial for Optimization Toolbox.
%##########################################################################
%
%   This is a demonstration script-file for the medium-scale algorithms
%   in the OPTIMIZATION TOOLBOX. It closely follows the Tutorial section 
%   of the users' guide.
%   
%   It consists of:
%           (1) unconstrained optimization example
%           (2) constrained optimization example 
%           (3) constrained example using gradients
%           (4) bounded example
%           (5) equality constrained example
%           (6) unconstrained example using user-supplied tolerances
%
%   All the principles outlined in this demonstration apply to the other
%   routines: fgoalattain, fminimax, lsqnonlin, fsolve.
%
%   The routines differ from the Tutorial Section examples in the User's
%   Guide only in that some objective functions are inline objects instead of
%   M-file functions. 
%
%##########################################################################

pause % Strike any key to continue (Note: use Ctrl-C to abort)
clc
%##########################################################################
%           DEMO 1: UNCONSTRAINED PROBLEM
%--------------------------------------------------------------------------
%
%   Consider initially the problem of finding a minimum to the function:
%
%                                2        2
%       f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
%
%--------------------------------------------------------------------------
pause % Strike any key to continue

% Create an inline object of the function to be minimized

fun = inline('exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1)');

pause % Strike any key to continue

% Take a guess at the solution

x0 = [-1 1];

pause % Strike any key to continue

% Set optimization options:
% Turn off the large-scale algorithms (the default):

options = optimset('LargeScale','off');

pause % Strike any key to start the optimization

[x, fval, exitflag, output] = fminunc(fun, x0, options);

%  The optimizer has found a solution at:
x
pause % Strike any key to continue

%  The function value at the solution is:
fval
pause % Strike any key to continue

%  The total number of function evaluations was: 
output.funcCount

pause % Strike any key for next demo
clc
%##########################################################################
%           DEMO 2: CONSTRAINED PROBLEM
%--------------------------------------------------------------------------
%
%   Consider the above problem with two additional constraints:
%
%                                      2        2
%   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
%
%   subject to  1.5 + x(1).x(2) - x(1) - x(2) <= 0
%                   - x(1).x(2)               <= 10
%
%--------------------------------------------------------------------------
pause % Strike any key to continue

% Objective Function (M-file)
type objfun

% Constraints Function (M-file)
type confun

pause % Strike any key to continue

% Take a guess at the solution

x0 = [-1 1];

pause % Strike any key to continue

% Set optimization options:
% Turn off the large-scale algorithms (the default)
%   and turn on the Display of results at each iteration:

options = optimset('LargeScale','off','Display','iter');

pause % Strike any key to start the optimization

% We have no linear equalities or inequalities or bounds, 
%   so pass [] for those arguments

[x,fval,exitflag,output] = fmincon('objfun',x0,[],[],[],[],[],[],'confun',options);

% A solution to this problem has been found at:
x 
pause % Strike any key to continue

% The function value at the solution is: 
fval 
pause % Strike any key to continue

% Both the constraints are active at the solution:
[c, ceq] = confun(x)
pause % Strike any key to continue

% The total number of function evaluations was: 
output.funcCount
pause % Strike any key for next demo
clc
%##########################################################################
%           DEMO 3: BOUNDED EXAMPLE
%--------------------------------------------------------------------------
%
%   Consider the above problem with additional bound constraints:
%
%                                      2        2
%   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
%
%   subject to  1.5 + x(1).x(2) - x(1) - x(2) <= 0
%                   - x(1).x(2)               <= 10
%
%          and                    x(1)        >= 0
%                                        x(2) >= 0
%
%--------------------------------------------------------------------------
pause % Strike any key to continue

% Objective Function (M-file)
type objfun

% Constraints Function (M-file)
type confun

pause % Strike any key to continue

% Again, make a guess at the solution
  
x0 = [-1 1];

pause % Strike any key to continue

% Set optimization options:
% Turn off the large-scale algorithms (the default):

options = optimset('LargeScale','off');

pause % Strike any key to continue

% This time we will use the bounded syntax:
% X = FMINCON(FUN, X0, [],[],[],[], LB, UB, OPTIONS, NONLCON) 

lb = zeros(1,2); % Lower bounds X >= 0
ub = [];         % No upper bounds 

pause % Strike any key to start the optimization

[x,fval,exitflag,output] = fmincon('objfun',x0,[],[],[],[],lb,ub,'confun',options);

% The solution to this problem has been found at:
x 
pause % Strike any key to continue

% The function value at the solution is: 
fval
pause % Strike any key to continue

% The constraint values at the solution are:
[c, ceq] = confun(x)
pause % Strike any key to continue

% The total number of function evaluations was: 
output.funcCount
pause % Strike any key for next demo
clc
%##########################################################################
%           DEMO 4: USER-SUPPLIED GRADIENTS
%--------------------------------------------------------------------------
%  The above problem can be solved more efficiently and accurately if
%  gradients are supplied by the user. This demo shows how this may be
%  performed.
%
%  Again the problem is:
%                                      2        2
%   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
%
%   subject to  1.5 + x(1).x(2) - x(1) - x(2) <= 0
%                   - x(1).x(2)               <= 10
%
%--------------------------------------------------------------------------
pause % Strike any key to continue

% Objective function and gradient (M-file) 
type objfungrad

% Constraints function and partial derivatives (M-file)
type confungrad 

pause % Strike any key to continue

% Make a guess at the solution:
 
x0 = [-1 1];

pause % Strike any key to continue

% Set optimization options:
% We can use the medium- or large-scale algorithm since we are providing 
% gradient information; we will continue to use the same algorithm for
% comparison purposes. 
options = optimset('LargeScale','off');

pause % Strike any key to continue

% We also set options to use the gradient information in the objective
% and constraint functions. Note: these options MUST be turned on or
% the gradient information will be ignored.

options = optimset(options,'GradObj','on','GradConstr','on');

pause % Strike any key to start the optimization

[x,fval,exitflag,output] = fmincon('objfungrad',x0,[],[],[],[],[],[],'confungrad',options);

% As before the solution to this problem has been found at:
x 
pause % Strike any key to continue

% The function value at the solution is: 
fval
pause % Strike any key to continue

% Both the constraints are active at the solution:
[c, ceq] = confungrad(x)
pause % Strike any key to continue

% The total number of function evaluations was: 
output.funcCount

pause % Strike any key for next demo
clc
%##########################################################################
%           DEMO 5: EQUALITY CONSTRAINED EXAMPLE
%--------------------------------------------------------------------------
%
%   Consider the above problem with an additional equality constraint:
%
%                                      2        2
%   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
%
%   subject to                  x(1)^2 + x(2)  = 1
%                              -x(1).x(2)     <= 10
%
%--------------------------------------------------------------------------
pause % Strike any key to continue

% Objective function
type objfun

% Constraints function (with equalities added)
type confuneq


pause % Strike any key to continue

% Again, make a guess at the solution

x0 = [-1 1];

pause % Strike any key to continue

% Set optimization options:
% Turn off the large-scale algorithms (the default):

options = optimset('LargeScale','off');

pause % Strike any key to start the optimization

[x,fval,exitflag,output] = fmincon('objfun',x0,[],[],[],[],[],[],'confuneq',options);

% The solution to this problem has been found at:
x 
pause % Strike any key to continue

% The function value at the solution is: 
fval
pause % Strike any key to continue

% The constraint values at the solution are:
[c, ceq] = confuneq(x)
pause % Strike any key to continue

% The total number of function evaluations was: 
output.funcCount

pause % Strike any key for next demo
clc
%##########################################################################
%           DEMO 6: CHANGING THE DEFAULT TERMINATION TOLERANCES
%--------------------------------------------------------------------------
%
%   Consider the original unconstrained problem:
%
%                                      2        2
%   minimize  f(x) = exp(x(1)) . (4x(1)  + 2x(2)  + 4x(1).x(2) + 2x(2) + 1)
%
%   This time we will solve it more accurately by overriding the 
%   default termination criteria (OPTIONS.TolX and OPTIONS.TolFun). 
%--------------------------------------------------------------------------
pause % Strike any key to continue

% Create an inline object of the function to be minimized

fun = inline('exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1)');

pause % Strike any key to continue

% Again, make a guess at the solution:

x0 = [-1 1];

pause % Strike any key to continue

% Set optimization options:
% Turn off the large-scale algorithms (the default):

options = optimset('LargeScale','off');

% Override the default termination criteria:
options = optimset(options,'TolX',1e-8);      % Termination tolerance on X
options = optimset(options,'TolFun',1e-8);    % Termination tolerance on F

pause % Strike any key to start the optimization

[x, fval, exitflag, output] = fminunc(fun, x0, options);

%  The optimizer has found a solution at:
x
pause % Strike any key to continue

%  The function value at the solution is:
fval
pause % Strike any key to continue

%  The total number of function evaluations was: 
output.funcCount
%   Note: warning messages may be displayed because of problems
%   in the calculation of finite difference gradients at the 
%   solution point. They are an indication that tolerances are overly 
%   stringent, however, the optimizer always continues to make steps 
%   towards the solution. 

pause % Strike any key to continue

% Set optimization options:
% Turn off the large-scale algorithms (the default):

options = optimset('LargeScale','off');

% If we want a tabular display of each iteration we can set
% options.Display = 'iter' as follows:

options = optimset(options, 'Display','iter');  

pause % Strike any key to start the optimization

[x, fval, exitflag, output] = fminunc(fun, x0, options);

%  The optimizer has found a solution at:
x
pause % Strike any key to continue

%  The function value at the solution is:
fval
pause % Strike any key to continue

%  The total number of function evaluations was: 
output.funcCount
%  At each major iteration the table displayed consists of: 
%           -   number of function values
%           -   function value
%           -   step length used in the line search
%           -   gradient in the direction of search
%

%------------------------------Demo End------------------------------------
pause % Strike any key for main menu
echo off
disp('End of demo')
