This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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.
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.
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 [ ].
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).
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.
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(.).
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] = ...
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.