Main Content

Optimize

Optimize or solve equations in the Live Editor

Description

The Optimize task lets you interactively optimize linear and nonlinear objective functions subject to constraints of various types, and to solve nonlinear systems of equations. The task automatically generates MATLAB® code for your live script.

Using this task, you can:

  • Choose a solver based on the characteristics of your problem. If you have Global Optimization Toolbox, you can choose to use its solvers as well.

  • Specify the objective and constraint functions, either by writing functions or browsing for functions.

  • Specify solver options.

  • Run the optimization.

For suggestions on how to use Optimize, see Use Optimize Live Editor Task Effectively. Currently, you cannot use the fseminf, GlobalSearch, or MultiStart solvers with Optimize.

For general information about Live Editor tasks, see Add Interactive Tasks to a Live Script.

Optimize task in Live Editor

Open the Task

To add the Optimize task to a live script in the MATLAB Editor, on the Live Editor Insert tab, select Task > Optimize.

Insert an Optimize Live Editor task

Alternatively, in a code block in the script, type a relevant keyword, such as optim or fmincon. Select Optimize from the suggested command completions.

Choosing Optimize from command completion suggestions

Parameters

Objective function type, specified by clicking the appropriate labeled button. The selected objective function determines which solvers are available and which solver is recommended for the problem (see Solver).

Constraint types, specified by clicking the appropriate labeled buttons. You can specify more than one constraint type. The selected constraints determine which solvers are available and which solver is recommended for the problem (see Solver).

Optimization solver that MATLAB uses to solve the problem, specified by selecting a solver from the list of available solvers. The available solvers and the recommended solver depend on your license and the selected Objective and Constraints.

Available Solvers

Constraint TypeObjective Type
LinearQuadraticLeast SquaresNonlinearNonsmooth
UnconstrainedSolution is trivialquadproglsqcurvefit, lsqnonlin, lsqnonneg, lsqlinfgoalattain, fminsearch, fminimax , fminunc, fsolve, fzero, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), particleswarm (Global Optimization Toolbox), simulannealbnd (Global Optimization Toolbox)fminsearch, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), particleswarm (Global Optimization Toolbox), simulannealbnd (Global Optimization Toolbox)
Bounds Onlylinprogquadproglsqcurvefit, lsqnonlin, lsqnonneg, lsqlinfgoalattain, fminbnd, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), particleswarm (Global Optimization Toolbox), simulannealbnd (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)fminbnd, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), particleswarm (Global Optimization Toolbox), simulannealbnd (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)
Linearlinprogquadproglsqlin, fminconfgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox)ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox)
Linear + Integerintlinprogga (Global Optimization Toolbox)ga (Global Optimization Toolbox)ga (Global Optimization Toolbox)ga (Global Optimization Toolbox)
Second-order coneconeprogfgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)fgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)fgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)
Nonlinearfgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)fgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)fgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)fgoalattain, fmincon, fminimax, ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)ga (Global Optimization Toolbox), gamultiobj (Global Optimization Toolbox), paretosearch (Global Optimization Toolbox), patternsearch (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)
Nonlinear + Integerga (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)ga (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)ga (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)ga (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)ga (Global Optimization Toolbox), surrogateopt (Global Optimization Toolbox)

Example: fmincon

Tips

  • For functions with extra inputs, Optimize requires you to choose the optimization variable, and to specify which workspace variables contain the fixed data inputs. For example, see Place Optimization Variables in One Vector and Data in Other Variables, which contains three function inputs:

    Optimization input = vars, Fixed input y = y, Fixed input w = w.

    Optimize generates code only after you specify all function inputs.

  • Optimize cannot parse a function containing the varargin input or a function that contains an error.

  • If you select an objective or nonlinear constraint function from a file, Optimize adds the file location to your MATLAB path.

  • If Optimize has a parsing error or if multiple local functions have the same name, the list of available local functions is empty.

Introduced in R2020b