2018 - Application of Fminsearch MATLAB Solver
2018 - Application of Fminsearch MATLAB Solver
min 𝑓(𝑥),
𝑥
x =
1.0000 1.0000
PRODEM
Fernando Pala Beirão Macedo Page 1 / 8
x =
1.0000 1.0000
x =
fval =
-5.9565e+04
PRODEM
Fernando Pala Beirão Macedo Page 2 / 8
options = optimset('Display','iter','PlotFcns',@optimplotfval);
Set an objective function and start point.
function f = objectivefcn1(x)
f = 0;
for k = -10:10
f = f + exp(-(x(1)-x(2))^2 - 2 * x(1)^2) * cos(x(2)) *
sin(2*x(2));
end
Include the code for objectivefcn1 as a file on the MATLAB path.
x0 = [0.25,-0.25];
fun = @objectivefcn1;
Obtain all solver outputs. Use these options to inspect the results after the solver finishes.
[x,fval,exitflag,output] = fminsearch(fun,x0,options)
Optimization terminated:
the current x satisfies the termination criteria using
OPTIONS.TolX of 1.000000e-04
and F(X) satisfies the convergence criteria using
OPTIONS.TolFun of 1.000000e-04
PRODEM
Fernando Pala Beirão Macedo Page 3 / 8
x =
-0.1696 -0.5086
fval =
-13.1310
exitflag =
output =
iterations: 35
funcCount: 69
algorithm: 'Nelder-Mead simplex direct search'
message: 'Optimization terminated:…'
PRODEM
Fernando Pala Beirão Macedo Page 4 / 8
fun = @(x)100*(x(2) - x(1)^2)^2 + (a - x(1))^2;
Put the parameter in the MATLAB workspace.
a=3;
Create an anonymous function of 𝑥 alone that includes the workspace value of the
parameter.
fun = @(x)f(x,a);
Solve the problem starting at 𝑥0 = [−1,1.9].
x0 = [-1,1.9]
x = fminsearch(fun,x0)
x =
3.0000 9.0000
Example: Minimize a Function specified by a File
Minimize an objective function whose values are given by executing a file. A function file
must accept a real vector 𝑥 and return a real scalar that is the value of the objective function.
Include the following code as a file named objectivefcn1.m on the MATLAB path.
function f = objectivefcn1(x)
f = 0;
for k = -10:10
f = f + exp(-(x(1)-x(2))^2 - 2 * x(1)^2) * cos(x(2)) *
sin(2*x(2));
end
Include the code for objectivefcn1 as a file on the MATLAB path.
x0 = [0.25,-0.25];
x = fminsearch(@objectivefcn1,x0);
x =
-0.1696 -0.5086
PRODEM
Fernando Pala Beirão Macedo Page 5 / 8
Initial point, specified as a real vector or real array, solvers use the number of elements in,
and size of, x0 to determine the number and size of variables that fun accepts,
Example: x0 = [1,2,3,4]
Data types: double
3. options – Optimization options (Structure such as optimset returns)
Optimization options, specified as a structure such as optimset returns. The optimset
can be used to set or change the values of these fields in the options structure,
3.1. Display – Level of display;
- notify – (default) displays output only if the function does not converge;
- final – displays just the final output;
- off/none – displays no output;
- iter – displays output at each iteration;
3.2. FunValCheck – Check whether objective functions are valid;
- on – displays an error when the objective function returns a value that is
complex or NaN;
- off – (default) displays no error;
3.3. MaxFunEvals – Maximum number of function evaluations allowed, a positive
integer. The default is 200 times the number of variables;
3.4. MaxIter – Maximum number of iterations allowed, a positive integer. The default
is 200 times the number of variables;
3.5. OutputFcn – Maximum number of iterations allowed, a positive integer. The
default is 200 times the number of variables;
3.6. PlotFuncs – Plot various measures of progress while the algorithm executes.
Select from predefined or write. Pass a function handle or a cell array of the function
handles. The default is none ([]);
- @optimplotx – plots the current point;
- @potimplotfunccount – plots the function count;
- @potimplotfval – plots the function value;
3.7. TolFun – Termination tolerance on the function value, a positive scalar. The default
is 10−4. Unlike other solvers, fminsearch stops when it satisfies both TolFun and
TolX;
3.8. TolX – Termination tolerance on 𝑥, a positive scalar. The default is 10−4. Unlike
other solvers, fminsearch stops when it satisfies both TolFun and TolX.
Example: options = optimset(‘Display’,’iter’)
Data types: struct
PRODEM
Fernando Pala Beirão Macedo Page 6 / 8
Solution, returned as a real vector or real array. The size of 𝑥 is the same as the size of 𝑥0 .
Typically, 𝑥 is a local solution to the problem when exitflag is positive.
2. fval – Objective function value (Real number)
Objective function value at the solution, returned as a real number. Generally,
fval=fun(x).
3. exitflag – Reason fminsearch stopped (Integer)
Reason fminsearch stopped, returned as an integer.
- 1 – the function converged to a solution 𝑥;
- 0 – number of iterations exceeded options.MaxIter or number of function
evaluations exceeded options.MaxFunEvals;
- -1 – the algorithm was terminated by the output function;
4. output – Information about the optimization process (Structure)
Information about the optimization process, returned a structure with fields.
- iterations – number of iterations;
- funcCount – number of function evaluations;
- algorithm – Nelder-Mead Simplex direct search;
- message – exit message;
The solver fminsearch uses the Simplex Search Method of Lagarias et al. This is a
direct search method that does not use numerical or analytic gradients as in fminunc. The
algorithm is not guaranteed to converge to a local minimum.
The solver fminsearch only minimizes over the real numbers, that is, the vector or array 𝑥 must
only consist of real numbers and 𝑓(𝑥) must only return real numbers. When 𝑥 has complex
values, 𝑥 should be split into real and imaginary parts.
The solver fminsearch is used to solve non-differentiable problems or problems with
discontinuities, particularly if no discontinuity occurs near the solution.
Algorithm for fminsearch
The algorithm for fminsearch uses a simplex 𝑛 + 1 points for 𝑛-dimensional vectors 𝑥. The
algorithm first makes a simplex around the initial guess 𝑥0 by adding 5% of each component
𝑥0 (𝑖) to 𝑥0 . The algorithm uses these 𝑛 vectors as elements of the simplex in addition to 𝑥0 . Then,
the algorithm modifies the simplex repeatedly according to the following procedure,
1. Let 𝑥(𝑖) denote the list of points in the current simplex, 𝑖 = 1, … , 𝑛 + 1;
2. Order the points in the simplex from lowest function value 𝑓(𝑥(1)) to the highest
𝑓(𝑥(𝑛 + 1)). At each step in the iteration, the algorithm discards the current worst
point 𝑥(𝑛 + 1), and accepts another point into the simplex;
3. Generate the reflected point,
𝑟 = 2𝑚 − 𝑥(𝑛 + 1),
where,
𝑥(𝑖)
𝑚=∑ , 𝑖 = 1, … , 𝑛,
𝑛
and calculate 𝑓(𝑥).
4. If 𝑓(𝑥(1)) ≤ 𝑓(𝑥) < 𝑓(𝑥(𝑛)), accept 𝑟 and terminate the iteration;
5. If 𝑓(𝑥) < 𝑓(𝑥(1)), calculate the expansion point 𝑠,
𝑠 = 𝑚 + 2 (𝑚 − 𝑥(𝑛 + 1)),
PRODEM
Fernando Pala Beirão Macedo Page 7 / 8
and calculate 𝑓(𝑥).
a) If 𝑓(𝑠) < 𝑓(𝑥), accept 𝑠 and terminate the iteration;
b) Otherwise, accept 𝑟 and terminate the iteration;
6. If 𝑓(𝑥) ≥ 𝑓(𝑥(𝑛)), perform a contraction between 𝑚 and the better of 𝑥(𝑛 + 1) and 𝑟.
a) If 𝑓(𝑟) < 𝑓(𝑥(𝑛 + 1)), that is, 𝑟 is better than 𝑥(𝑛 + 1), calculate,
𝑟+𝑚
𝑐 =𝑚+ ,
2
and calculate𝑓(𝑐). If 𝑓(𝑐) < 𝑓(𝑟), accept 𝑐 and terminate the iteration.
Otherwise, continue with step 7 (shrink);
b) If 𝑓(𝑟) ≥ 𝑓(𝑥(𝑛 + 1)), calculate,
𝑥(𝑛 + 1) − 𝑚
𝑐𝑐 = 𝑚 + ,
2
and calculate 𝑓(𝑐𝑐). If 𝑓(𝑐𝑐) < 𝑓(𝑥(𝑛 + 1)), accept 𝑐𝑐 and terminate the
iteration. Otherwise, continue with step 7 (shrink);
7. Calculate the 𝑛 points,
𝑥(𝑖) − 𝑥(1)
𝑣(𝑖) = 𝑥(1) + ,
2
and calculate𝑓(𝑣(𝑖)), 𝑖 = 2, … , 𝑛 + 1. The simplex at the next iteration is
𝑥(1), 𝑣(2), … , 𝑣(𝑛 + 1).
PRODEM
Fernando Pala Beirão Macedo Page 8 / 8