initiation to matlab - lecture 5
TRANSCRIPT
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Initiation to Matlab - Lecture 5
Ludovic Calès1
1HEC - University of [email protected] - Room 253
M.Sc. Finance - Autumn 2012
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
This Course Outline
TodayFunction HandleSolve 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
ProblemThe optimization functions take the function to optimize asan input.However, a function is not a variable and therefore cannotbe passed in argument.
Solution: Function HandleThe function to optimize is transformed into a variable ofthe data type function handle.The handle, i.e. the variable representing a function, isobtained with the ’at’ sign (@) followed by the functionname.
myVariable = @FunctionName (1)
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example
ExampleA function handle for the cosine function – named cos in Matlab– is built as follows:
>> myCosineHandle = @cos;
Now, the variable myCosineHandle can be passed in argumentto another function. Remark that the function handle is notused to call the related function but to handle it as simply asany other variable.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
fsolve(.)
Definition
The function fsolve(.) solve the equation f (X ) = ~0 where X and f (X )are vectors.
Syntax
[X , fval ,exitflag] = fsolve(myfcthandle,X0) (2)
where
myfcthandle function handle of fX0 initial valuesexitflag > 0 , succeeded,
< 0 , failed,0 , did not end (e.g. it reaches the max.
number of iterations),X solutionfval value of f in X .
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example
ProblemSolve the following problem
2x5 + 3x4 − 30x3 − 57x2 − 2x + 24 = 0
Solution1 Write the .m file 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.56fval =
-3.324e-009exitflag =
1
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
fminsearch(.)
DefinitionThe function fminsearch(.) to find the minimum of a multivariatefunction f (x) returning a scalar, i.e. solves the problem min
xf (x)
Syntax
[X , fval ,exitflag] = fminsearch(myfcthandle,X0) (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) = x2 + (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
Solution1 Write the .m file 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 2fval =
2.7371e-009exitflag =
1
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
fmincon(.)
Definition
The function fmincon(.) solves the following minimization problem
minx
f (x) subject to
∣∣∣∣∣∣∣∣∣c(x) ≤ 0ceq(x) = 0A x ≤ bAeq x = beqlb ≤ x ≤ ub
(4)
where c(x) and ceq(x) are non-linear functions, A and Aeq are matrices, b, beq, lband ub are vectors.
Syntax
[X , fval, exitflag] = fmincon(myfcthandle,X0,A, b,Aeq, beq, lb, ub, nonlcon) (5)
where the arguments A, b, Aeq, beq, lb and ub are those given by the problem andnonlcon defines the functions c(x) and ceq(x). If some arguments do not apply, setthem to [ ].
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example: Himmelblau’s function
Problem
Find the minimum of f (x , y) = (x2 + y − 11)2 + (x + y2 − 7)2 such that x < 0and y > 0. Here, the system implied by the constraints is[
1 00 −1
]︸ ︷︷ ︸
A
[xy
]≤[
00
]︸ ︷︷ ︸
b
(6)
Figure: Surface and contour plots of Himmelblau’s function.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Example: Himmelblau’s function
Solution
1 Write the .m file 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 Define 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(.)
DefinitionThe function optimset(.) modifies the optimization parameters.It takes as input a parameter name as a string followed by thevalue assigned to this parameter, and returns a structure whichis then provided as last argument to the functions fsolve(.),fminsearch(.) and fmincon(.).
Optimization Parameters’MaxIter’ : the number of iteration performed by thealgorithm (1000 by default)’TolFun’ : the precision desired for the result returned bythe objective function (10−6 by default)’TolX’ : the precision desired for the variables (10−6 bydefault).
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
CaseSuppose that your objective function takes some parameters inaddition to its unknown variables. To pass these parameters asinput of the objective function while optimizing it, we use thefunction handle precising which input is the variable.
Syntax
OptimizationFct(@(x)myObjectiveFct(x ,a,b), . . . )
where x is the unknown variable, a and b are parameterspassed as input to my objective function. myObjectiveFct(.) isthe objective function and OptimizationFct(.) is theoptimization 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) = ax2 + bx + c where x is theunknown and, a, b and c are parameters. We want to find theminimum of mypoly(.) using fminsearch(.) and passing a, band c as input of mypoly(.).
1 The .m file of this function would befunction y = mypoly(x, a, b, c)
y = a*xˆ2 + b*x + cend
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 − x2)2
(Rosenbrock’s function).
Figure: Surface and contour plots of Rosenbrock’s function.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Solution
SolutionThe minimum of Rosenbrock’s function is (1,1).
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Exercise 2
Exercise 2Find the minimum of f (x , y) = 20 + x2 + y2 − 10 (cos 2πx + cos 2πy)(Rastrigin’s function).
Figure: Surface and contour plots of Rastrigin’s function.
Introduction Function Handle fsolve(.) fminsearch(.) fmincon(.) Complements Exercises Surface & Contour Plots
Solution
SolutionThe minimum of Rastrigin’s function is (0,0). However, thisfunction is known to be very difficult to solve and theminimization functions proposed by Matlab cannot find 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.