主要内容

优化

优化或者solve equations in the Live Editor

描述

The优化任务使您可以在两种方式之间选择交互式优化问题或求解方程式的非线性系统:

  • Problem-based (recommended) — Create symbolic optimization variables and expressions to represent the objective function and constraints or equations.

  • 求解器-based — Represent the objective function and constraints or equations using standard MATLAB®code.

该任务自动为您的实时脚本生成MATLAB代码。

使用此任务的基于问题的版本,您可以:

  • Specify optimization variable arrays, including their bounds and initial values.

  • 指定问题类型:最小化,最大化,可行性或方程解决。

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

  • Optionally, choose a solver, and specify nondefault options..

  • 运行优化。

Using the solver-based version of this task, you can:

  • 根据问题的特征选择求解器。如果你有全局优化工具箱,您也可以选择使用其求解器。

  • 通过编写功能或浏览功能来指定目标和约束功能。

  • 指定求解器选项。

  • 运行优化。

开始使用优化, see开始使用基于求解器的优化实时编辑器任务从基于问题的优化实时编辑器任务开始。有关如何使用的建议优化, see有效地使用基于求解器的优化实时编辑器任务或者使用基于问题的优化实时编辑器任务有效。目前,you cannot use thefseminf,GlobalSearch, orMultiStart求解器优化

For general information about Live Editor tasks, see将交互式任务添加到实时脚本

Open the Task

To add the优化task to a live script in the MATLAB Editor, on the Live EditorInsert选项卡,选择Task > Optimize

插入优化实时编辑器任务

Alternatively, in a code block in the script, type a relevant keyword, such as最佳或者fmincon。选择优化from the suggested command completions.

从命令完成建议中选择优化

Parameters

基于问题的变量名称是动态的;您可以选择您喜欢的任何名称,但要按照通常的命名限制(请参阅可变名称)。

Problem-Based

问题类型,通过单击适当的标记按钮指定。

客观的function, specified as an optimization expression, a local function, or a function file. Applies when the目标Minimize或者Maximize

约束on solution, specified as an optimization expression, local function, or function file. Add constraints to the problem by clicking the + button.

问题方程式,指定为优化表达式,本地函数或功能文件。适用于目标求解方程。通过单击 +按钮将方程式添加到问题中。

State of task, specified asDefine problem或者Solve problem。For more information, see使用基于问题的优化实时编辑器任务有效

求解器-Based

目标函数类型,通过单击适当的标记按钮指定。选定的目标函数确定可用的求解器,并建议使用哪个求解器(请参阅求解器)。

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求解器)。

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客观的约束

限制

  • 目前,优化has the following restrictions for multiobjective optimization.

    • You must specify your objective functions using a single function with multiple outputs. In other words, your objective function must output a vector of values, one entry for each objective.

    • 所有目标函数必须使用相同的意义上说,minimization for the solver-based task, and either minimization or maximization for the problem-based task.

提示

  • For functions with extra inputs, the solver-based and problem-based优化任务的要求有所不同。

    • 求解器-Based: Choose the optimization variable, and specify which workspace variables contain the fixed data inputs. For example, seePlace Optimization Variables in One Vector and Data in Other Variables, which contains three function inputs:

      优化输入= var,固定输入y = y,固定输入w = w。

      优化generates code only after you specify all function inputs.

    • Problem-Based:为每个函数输入指定优化变量或工作区变量名称。如果函数签名中的输入参数名称匹配现有优化变量或工作区变量名称,则优化自动选择该名称。

      函数输入= x,y,a。

      优化generates code only after you specify all function inputs.

  • 优化cannot parse a function containing thevarargininput or a function that contains an error.

  • 如果您从文件中选择一个函数,优化adds the file location to your MATLAB path.

  • 如果优化has a parsing error or if multiple local functions have the same name, the list of available local functions is empty.

Version History

在R2020b中引入

See Also

Functions