Matlab - Slides - Lecture 5 + Sol
Matlab - Slides - Lecture 5 + Sol
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.