Main Content

Optimize

Optimize in the Live Editor

Description

TheOptimizetask lets you interactively optimize a nonlinear objective function or solve a nonlinear equation. The task automatically generates MATLAB®code for your live script.

Using this task, you can:

  • Specify the objective function, either by writing a function or browsing for a function.

  • Specify solver options.

  • Run the optimization.

If you have Optimization Toolbox™ orGlobal Optimization Toolboxyou can solve more problem types using theOptimize任务,如解决系统的非线性方程s or optimize an objective function with nonlinear constraints. For general information about Live Editor tasks, seeAdd Interactive Tasks to a Live Script.

Open the Task

To add theOptimizetask to a live script in the MATLAB Editor, on the Live EditorInserttab, selectTask > Optimize.

Insert Optimize Live Editor task

Alternatively, in a code block in the script, type a relevant keyword, such asfminbnd. SelectOptimize从建议命令完成。

Choosing Optimize from command completion suggestions

Parameters

Optimization solver that MATLAB uses to solve the problem, specified as one of these solvers:

  • fminbnd— Minimize a real function of one variable on a bounded domain.

  • fminsearch— Minimize a real function of several variables on an unbounded domain.

  • lsqnonneg— Minimizenorm(C*x-d)subject tox ≥ 0.

  • fzero— Find a real pointxwhere a real nonlinear functionf(x) = 0.

Example:fminsearch

Function to minimize (forfminbndorfminsearch) or to find a root (forfzero), specified as a function handle, local function name, or function file.

  • Function handle — Create the function handle in the workspace. Then, in theOptimizetask, selectObjective function > Function handleand choose the objective function.

  • Local function

    • New — In theOptimizetask, selectObjective function > Local functionand then click theNewbutton. A new function definition appears below the task. Edit the function so that it calculates the objective. Then choose the local function.

    • Existing — SelectObjective function > Local functionand choose the local function.

  • Function file

    • New — In theOptimizetask, selectObjective function > From fileand then click theNewbutton. A new function file appears. Edit the file to calculate the objective, then save the file on the MATLAB path. Click theBrowsebutton and select the file.

    • Existing — ChooseFrom filethenBrowse...and select the file.

Tips

  • For functions with extra inputs,Optimizerequires you to choose the optimization variable, and to specify which workspace variables contain the fixed data inputs. This example contains three function inputs:

    Optimization input =

    Optimizegenerates code only after you specify all function inputs.

  • Optimizecannot parse a function containing thevarargininput or a function that contains an error.

  • If you select an objective function from a file,Optimizeadds the file location to your MATLAB path.

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

Version History

Introduced in R2020b

See Also

Functions