Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

优化

在实时编辑器中优化或求解方程

说明

优化任务允许您基于各种类型的约束以交互方式优化线性和非线性目标函数,并求解非线性方程组。该任务会自动为您的实时脚本生成 MATLAB® 代码。

使用此任务,您可以:

  • 根据问题的特征选择一个求解器。如果您有 Global Optimization Toolbox,也可以选择使用它的求解器。

  • 通过编写函数或浏览查找函数来指定目标函数和约束函数。

  • 指定求解器选项。

  • 运行优化。

有关如何使用优化任务的建议,请参阅Use Optimize Live Editor Task Effectively。当前,您无法将 fseminfGlobalSearchMultiStart 求解器与优化任务结合使用。

有关实时编辑器任务的一般信息,请参阅将交互式任务添加到实时脚本中

Optimize task in Live Editor

打开任务

要将优化任务添加到 MATLAB 编辑器中的实时脚本,请在实时编辑器的插入选项卡上,选择任务 > 优化

Insert an Optimize Live Editor task

或者,在脚本的代码块中,键入相关关键字,如 optimfmincon。从建议的命令自动填充项中选择优化

Choosing Optimize from command completion suggestions

参数

目标函数类型,通过点击对应的标签按钮来指定。所选目标函数确定哪些求解器可用,以及为该问题推荐哪个求解器(请参阅 Solver)。

约束类型,通过点击对应的标签按钮来指定。您可以指定多个约束类型。所选约束确定哪些求解器可用,以及为该问题推荐哪个求解器(请参阅 Solver)。

MATLAB 用于求解问题的优化求解器,通过从可用求解器列表中选择来指定。可用求解器和推荐求解器取决于您的许可证以及所选 ObjectiveConstraints

可用求解器

约束类型目标类型
线性二次最小二乘非线性非平滑
无约束平凡解quadproglsqcurvefitlsqnonlinlsqnonneglsqlinfgoalattainfminsearchfminimaxfminuncfsolvefzeroga (Global Optimization Toolbox)gamultiobj (Global Optimization Toolbox)paretosearch (Global Optimization Toolbox)patternsearch (Global Optimization Toolbox)particleswarm (Global Optimization Toolbox)simulannealbnd (Global Optimization Toolbox)fminsearchga (Global Optimization Toolbox)gamultiobj (Global Optimization Toolbox)paretosearch (Global Optimization Toolbox)patternsearch (Global Optimization Toolbox)particleswarm (Global Optimization Toolbox)simulannealbnd (Global Optimization Toolbox)
仅边界linprogquadproglsqcurvefitlsqnonlinlsqnonneglsqlinfgoalattainfminbndfminconfminimaxga (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)fminbndga (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)
线性linprogquadproglsqlinfminconfgoalattainfminconfminimaxga (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)
线性 + 整数intlinprogga (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)
二阶锥coneprogfgoalattainfminconfminimaxga (Global Optimization Toolbox)gamultiobj (Global Optimization Toolbox)paretosearch (Global Optimization Toolbox)patternsearch (Global Optimization Toolbox)surrogateopt (Global Optimization Toolbox)fgoalattainfminconfminimaxga (Global Optimization Toolbox)gamultiobj (Global Optimization Toolbox)paretosearch (Global Optimization Toolbox)patternsearch (Global Optimization Toolbox)surrogateopt (Global Optimization Toolbox)fgoalattainfminconfminimaxga (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)
非线性fgoalattainfminconfminimaxga (Global Optimization Toolbox)gamultiobj (Global Optimization Toolbox)paretosearch (Global Optimization Toolbox)patternsearch (Global Optimization Toolbox)surrogateopt (Global Optimization Toolbox)fgoalattainfminconfminimaxga (Global Optimization Toolbox)gamultiobj (Global Optimization Toolbox)paretosearch (Global Optimization Toolbox)patternsearch (Global Optimization Toolbox)surrogateopt (Global Optimization Toolbox)fgoalattainfminconfminimaxga (Global Optimization Toolbox)gamultiobj (Global Optimization Toolbox)paretosearch (Global Optimization Toolbox)patternsearch (Global Optimization Toolbox)surrogateopt (Global Optimization Toolbox)fgoalattainfminconfminimaxga (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)
非线性 + 整数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)ga (Global Optimization Toolbox)surrogateopt (Global Optimization Toolbox)

示例: fmincon

提示

  • 对于具有额外输入的函数,优化要求您选择优化变量,并指定哪些工作区变量包含固定数据输入。有关示例,请参阅Place Optimization Variables in One Vector and Data in Other Variables,其中包含三个函数输入:

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

    优化仅在您指定所有函数输入后才生成代码。

  • 优化无法解析包含 varargin 输入的函数或包含错误的函数。

  • 如果从文件中选择目标或非线性约束函数,优化会将文件位置添加到您的 MATLAB 路径中。

  • 如果优化出现解析错误,或多个局部函数同名,则可用局部函数列表为空。

在 R2020b 中推出