Although the Optimization app affords convenient ways to set options and run problems, it will be removed in a future release. This topic describes other ways to accomplish optimization tasks without using the app.
Set options easily — Set Options Using Live Scripts or Set Options: Command Line or Standard Scripts
Monitor the optimization — Choose Plot Functions
Pass solver arguments correctly — Pass Solver Arguments
Beginning with R2018a, live scripts show suggestions for
optimoptions
names and values.
On the Home tab, in the File section, click New Live Script to create a live script.
In the Live Editor, set options by entering options = optimoptions(
. MATLAB® shows a list of solvers.
Select a solver, then enter a comma. MATLAB displays a list of name-value pairs for the solver.
Select a name-value pair in one of these ways:
Click the name-value pair.
Use the arrow keys to highlight the name-value pair, and then press Tab.
Type the first letters of the name-value pair, and then press Tab.
Enter the appropriate value for the selected name. If the value is from a list of choices, you can select it the same way that you selected the name.
Continue adding name-value pairs until the options are complete.
Be sure to pass the options to your solver.
[x,fval,exitflag,output] = ... fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nlcon,options)
Tip
For help choosing a solver, see Optimization Decision Table.
For help choosing the solver algorithm, see Choosing the Algorithm.
To understand the meaning of other options, see Set Options.
Beginning with R2018a, the MATLAB command line and the standard Editor show suggestions for
optimoptions
names and values.
Set options by entering options =
optimoptions('
and pressing Tab. MATLAB shows a list of solvers.
Select a solver in one of these ways:
Double-click the solver.
Use the arrow keys to highlight the solver, and then press Tab.
Type the first letters of the solver, and then press Tab.
Enter ','
and then press Tab.
MATLAB displays a list of name-value pairs for the solver.
Select a name-value pair using one of the ways described in step 2.
Enter the appropriate value for the selected name. If the value is from a list of choices, you can select it the same way that you selected the name.
Continue adding name-value pairs until the options are complete.
Be sure to pass the options to your solver.
[x,fval,exitflag,output] = ... fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nlcon,options)
Tip
For help choosing a solver, see Optimization Decision Table.
For help choosing the solver algorithm, see Choosing the Algorithm.
To understand the meaning of other options, see Set Options.
To monitor your optimization while it runs, use a plot function. Solvers have a
set of built-in plot functions. Use optimoptions
to set the
'PlotFcn'
name-value pair to a built-in plot function, a cell
array of built-in plot functions, or a function handle or cell array of function
handles to plot functions.
Choose plot functions using live scripts:
To choose plot functions using the Editor or the command line, enter options =
optimoptions('
and
then press Tab:solvername
','PlotFcn',{'
To choose a custom plot function, pass a function handle such as
@myplotfun
. Custom plot functions use the same syntax as
output functions. See Output Functions for Optimization Toolbox™ and Output Function and Plot Function Syntax.
linprog
, lsqlin
,
quadprog
, and lsqnonneg
do not support
plot functions, because these solvers typically run quickly. To monitor their
progress, you can use iterative display for linprog
, the
lsqlin
'interior-point'
algorithm, and the quadprog
'interior-point-convex'
algorithm. Set the
'Display'
option to 'iter'
.
The fminbnd
, fminsearch
, and
fzero
solvers do not use options created by
optimoptions
, only optimset
. To see
which plot functions these solvers use, consult their reference pages:
Solvers use positional function arguments. For example, the syntax for
fmincon
arguments is
fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
If you want to include only fun
, x0
,
lb
, and options
arguments, then the
appropriate syntax is
fmincon(fun,x0,[],[],[],[],lb,[],[],options)
Sometimes the inexperienced will, instead, write
fmincon(fun,x0,lb,options) % This is incorrect!
This call throws an error. In this incorrect command, fmincon
interprets the lb
argument as representing the
A
matrix, and the options
argument as
representing the b
vector. The third argument always represents
the A
matrix, and the fourth argument always represents the
b
vector.
It can be difficult to keep track of positional arguments as you enter a command. The following are suggestions for obtaining the correct syntax.
Use live scripts. As you enter a command, you see function hints that
guide you to enter the correct argument in each position. Enter
[]
for unused arguments.
Use the MATLAB Editor or command line. As you enter commands, you see lists
of proper syntax that guide you to enter the correct argument in each
position. Enter []
for unused arguments.
Create a problem
structure. This way, you can pass
fewer arguments and pass named arguments instead of positional arguments.
For fmincon
, the problem
structure
requires at least the objective
, x0
,
solver
, and options
fields. So, to
give only the fun
, x0
,
lb
, and options
arguments, create
a problem
structure as follows:
% These commands assume that fun, x0, lb, and opts exist prob.objective = fun; prob.x0 = x0; prob.lb = lb; prob.options = opts; prob.solver = 'fmincon';
You can also create a problem
structure using one
struct
command.
% This command assumes that fun, x0, lb, and opts exist prob = struct('objective',fun,'x0',x0,'lb',lb,... 'options',opts,'solver','fmincon')
If you have Global Optimization Toolbox, you can create a problem structure for
fmincon
, fminunc
,
lsqcurvefit
, and lsqnonlin
by
using createOptimProblem
(Global Optimization Toolbox).