# Optimization

The optimization toolbox contains a number of routines that use numerical techniques to find extremum of user-supplied functions.

MATLAB uses the Newton-Raphson algorithm most of the cases which uses derivatives to find the minimum of a function, That is, if you want to maximize a function f, you will minimize -f, a function with the minimum at the same point as the maximum of f .

To use MATLAB to optimize function, the likelihood of a model, a custom function that returns the function value at a set of parameters must be constructed. All optimization targets must have the parameters as the first argument.

For example consider finding the minimum of x2,

function x2 = minx2(x) x2=x^2;

when we use multiple parameters (vector), the objective function have the form

function obj = minf(params) x=params(1); y=params(2); obj= x^2-3*x+3+y*x-3*y+y^2;

we can obviously add additional input such as variable

function obj = minf(params,variable1) x=params(1); y=params(2); obj= x^2 + 3x*variable1 + y^2;

In econometrics in our likelihood function we need at least two inputs: parameters and data.

Once we have specified the function to optimize, we use one of MATLAB optimizers,

- fminsearch,
- fminbnd
- fminunc
**fmincon**

## fmincon

fmincon performs constrained minimizations of f(x), using linear and/or nonlinear constraints which can be either equality of inequality constraints.

The generic form of fmincon is,

[p,fval,exitflag]=fmincon(’fun’, p0,A,b, AEQ,bEQ,LB, UB,nlcon,options,var1,var2,…)

## optimset

optimset sets optimization options and has two distinct forms. The initial call to optimzet should always be of the form

options = optimset(’fmintype’)

which will return the default options for that type.

Once the options structure has been initialized, individual options can be changes by calling

options = optimset(options,’option1’,option value1,’option2’,option value2,. . .)

For example, to set options for fmincon,

>> options = optimset(’fmincon’); >> options = optimset(options,’MaxFunEvals’,1000,’MaxIter’,1000); >> options = optimset(options,’TolFun’,1e-3);

where,

*MaxFunEvals*, Maximum number of function evaluations allowed.*MaxFunEvals*, Maximum number of iterations allowed.*TolFun*, Termination tolerance on the function value.

See the help optimset for more details.