0% found this document useful (0 votes)
114 views

Matlab - Slides - Lecture 5 + Sol

This document discusses optimization techniques in MATLAB, including function handles, fsolve, fminsearch, and fmincon. It begins with an introduction of function handles, which allow functions to be passed as arguments. It then covers fsolve for solving equations, fminsearch for unconstrained optimization, and fmincon for constrained optimization. Examples are provided for each function using standard test functions like the bowl function. Additional topics covered include modifying optimization parameters and passing extra parameters to objective functions. Exercises demonstrate applications of the optimization techniques.

Uploaded by

Ktk Zad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views

Matlab - Slides - Lecture 5 + Sol

This document discusses optimization techniques in MATLAB, including function handles, fsolve, fminsearch, and fmincon. It begins with an introduction of function handles, which allow functions to be passed as arguments. It then covers fsolve for solving equations, fminsearch for unconstrained optimization, and fmincon for constrained optimization. Examples are provided for each function using standard test functions like the bowl function. Additional topics covered include modifying optimization parameters and passing extra parameters to objective functions. Exercises demonstrate applications of the optimization techniques.

Uploaded by

Ktk Zad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.

) Complements Exercises Surface & Contour Plots


Initiation to Matlab - Lecture 5
Ludovic Cals
1
1
HEC - University of Lausanne
[email protected]
Extranef - Room 253
M.Sc. Finance - Autumn 2012
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
This Course Outline
Today
Function Handle
Solve an equation with fsolve(.)
Minimize an unconstrained problem with fminsearch(.)
Minimize a constrained problem with fmincon(.)
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Function Handle
Problem
The optimization functions take the function to optimize as
an input.
However, a function is not a variable and therefore cannot
be passed in argument.
Solution: Function Handle
The function to optimize is transformed into a variable of
the data type function handle.
The handle, i.e. the variable representing a function, is
obtained with the at sign (@) followed by the function
name.
myVariable = @FunctionName (1)
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example
Example
A function handle for the cosine function named cos in Matlab
is built as follows:
>> myCosineHandle = @cos;
Now, the variable myCosineHandle can be passed in argument
to another function. Remark that the function handle is not
used to call the related function but to handle it as simply as
any other variable.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
fsolve(.)
Denition
The function fsolve(.) solve the equation f (X) =

0 where X and f (X)
are vectors.
Syntax
[X, fval , exitag] = fsolve(myfcthandle, X
0
) (2)
where
myfcthandle function handle of f
X
0
initial values
exitag > 0 , succeeded,
< 0 , failed,
0 , did not end (e.g. it reaches the max.
number of iterations),
X solution
fval value of f in X.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example
Problem
Solve the following problem
2x
5
+ 3x
4
30x
3
57x
2
2x + 24 = 0
Solution
1
Write the .m le of the function to solve:
function y = myFunction(x)
y = 2
*
x.5 + 3
*
x.4 - 30
*
x.3 - 57
*
x.2 - 2
*
x + 24
end
2
use the fsolve(.) function with a guessed initial value.
>> [X , fval, exitflag] = fsolve(@myFunction,0)
X =
0.56
fval =
-3.324e-009
exitflag =
1
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
fminsearch(.)
Denition
The function fminsearch(.) to nd the minimum of a multivariate
function f (x) returning a scalar, i.e. solves the problem min
x
f (x)
Syntax
[X, fval , exitag] = fminsearch(myfcthandle, X
0
) (3)
where the arguments are those of the previous section.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example: Bowl function
Problem
Find the minimum of f (x, y) = x
2
+ (y 2)
2
, the bowl function.
Figure: Surface and contour plots of the bowl function.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example: Bowl function
Solution
1
Write the .m le for the bowl function:
function z = Bowl(x)
z = x(1)2 + (x(2) -2)2;
end
2
Call fminsearch(.) as follows
>> [ X , fval , exitflag ] = fminsearch(@Bowl, [ 1 1 ]);
X =
-0.0000 2
fval =
2.7371e-009
exitflag =
1
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
fmincon(.)
Denition
The function fmincon(.) solves the following minimization problem
min
x
f (x) subject to

c(x) 0
ceq(x) = 0
A x b
Aeq x = beq
lb x ub
(4)
where c(x) and ceq(x) are non-linear functions, A and Aeq are matrices, b, beq, lb
and ub are vectors.
Syntax
[X, fval , exitag] = fmincon(myfcthandle, X
0
, A, b, Aeq, beq, lb, ub, nonlcon) (5)
where the arguments A, b, Aeq, beq, lb and ub are those given by the problem and
nonlcon denes the functions c(x) and ceq(x). If some arguments do not apply, set
them to [ ].
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example: Himmelblaus function
Problem
Find the minimum of f (x, y) = (x
2
+y 11)
2
+ (x +y
2
7)
2
such that x < 0
and y > 0. Here, the system implied by the constraints is

1 0
0 1

x
y

0
0


b
(6)
Figure: Surface and contour plots of Himmelblaus function.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example: Himmelblaus function
Solution
1
Write the .m le for the Himmelblau function.
function z = Himmelblau(x)
z = (x(1) 2+x(2)-11) 2 + (x(1)+x(2) 2-7) 2;
end
2
Dene the parameters A and b as follows.
>> A = [ 1 0 ; 0 -1];
>> b = [ 0 ; 0 ];
3
Call fmincon(.) as follows
>> [ X , fval , exitflag ] = fmincon(@Himmelblau,...
[ -1 1 ], A, b, [], [], [], [], []);
X =
-2.8050 3.1313
fval =
4.0509e-007
exitflag =
5
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Modify Optimization Parameters with optimset(.)
Denition
The function optimset(.) modies the optimization parameters.
It takes as input a parameter name as a string followed by the
value assigned to this parameter, and returns a structure which
is then provided as last argument to the functions fsolve(.),
fminsearch(.) and fmincon(.).
Optimization Parameters
MaxIter : the number of iteration performed by the
algorithm (1000 by default)
TolFun : the precision desired for the result returned by
the objective function (10
6
by default)
TolX : the precision desired for the variables (10
6
by
default).
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Modify Optimization Parameters with optimset(.)
Syntax
options = optimset (

Param1

, value1,

Param2

, value2, . . . )
(7)
Example with fminsearch(.)
>> options = optimset( TolFun, 1e-10);
>> [X, fval, exitflag] = fminsearch(@Bowl, [ 1 1 ], options);
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Pass Extra Parameters to the Objective Functions
Case
Suppose that your objective function takes some parameters in
addition to its unknown variables. To pass these parameters as
input of the objective function while optimizing it, we use the
function handle precising which input is the variable.
Syntax
OptimizationFct (@(x)myObjectiveFct (x, a, b), . . . )
where x is the unknown variable, a and b are parameters
passed as input to my objective function. myObjectiveFct(.) is
the objective function and OptimizationFct(.) is the
optimization function, i.e. fsolve(.), fminsearch(.), fmincon(.).
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Pass Extra Parameters to the Objective Functions
Example
Consider the function mypoly(x) = ax
2
+ bx + c where x is the
unknown and, a, b and c are parameters. We want to nd the
minimum of mypoly(.) using fminsearch(.) and passing a, b
and c as input of mypoly(.).
1
The .m le of this function would be
function y = mypoly(x, a, b, c)
y = a
*
x2 + b
*
x + c
end
2
We set a, b and c and call fminsearch(.) as follows
>> a=1; b=2; c=1;
>> [X, fval, exitflag] = ...
fminsearch(@(x)mypoly(x, a, b, c), 1);
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Exercise 1
Exercise 1
Find the minimum of f (x, y) = (1 x)
2
+ 100 (y x
2
)
2
(Rosenbrocks function).
Figure: Surface and contour plots of Rosenbrocks function.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Solution
Solution
The minimum of Rosenbrocks function is (1, 1).
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Exercise 2
Exercise 2
Find the minimum of f (x, y) = 20 + x
2
+ y
2
10(cos 2x + cos 2y)
(Rastrigins function).
Figure: Surface and contour plots of Rastrigins function.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Solution
Solution
The minimum of Rastrigins function is (0, 0). However, this
function is known to be very difcult to solve and the
minimization functions proposed by Matlab cannot nd it easily.
They require initial values near the minimum.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Surface & Contour Plots
Surface & Contour Plots
The function surf(X,Y,Z) plots a surface graph with Z = f(X,Y).
The function contour(X,Y,Z,n) plots a coutour graph with Z =
f(X,Y) and n is the number of contour lines.

You might also like