本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。
此示例说明如何将 Optimization 工具与 “fmincon
” 求解器结合使用,以在受限于线性和非线性约束和边界的情况下对二次问题求最小值。
注意
Optimization 工具会显示警告,说明它将在以后的版本中被删除。
设想一个问题,它需要找到能够求解以下问题的 [x1, x2]:
需满足以下约束
此问题的初始估计值是 x1 = 3 和 x2 = 1。
function f = objecfun(x) f = x(1)^2 + x(2)^2;
function [c,ceq] = nonlconstr(x) c = [-x(1)^2 - x(2)^2 + 1; -9*x(1)^2 - x(2)^2 + 9; -x(1)^2 + x(2); -x(2)^2 + x(1)]; ceq = [];
在命令行窗口中输入 optimtool
以打开 Optimization 工具。
从待选求解器中选择 “fmincon
”,并将 Algorithm 字段更改为 Active set
。
在 Objective function 字段中输入 @objecfun
以调用 objecfun.m
文件。
在 Start point 字段中输入 [3;1]
。
定义约束。
通过在 Lower 字段中输入 [0.5,-Inf]
,设置边界 0.5
≤ x1。-Inf
意味着 x2 没有下界。
通过在 A 字段中输入 [-1
-1]
设置线性不等式约束,并在 b 字段中输入 -1
。
通过在 Nonlinear constraint function 字段中输入 @nonlconstr
,设置非线性约束。
在 Options 窗格中,根据需要展开 Display to command window 选项,并选择 “Iterative
” 以在命令行窗口中显示每次迭代的算法信息。
点击 Start 按钮,如下图所示。
当算法终止时,在 Run solver and view results 下,将显示以下信息:
算法终止时的 Current iteration 值,本例中为 7
。
算法终止时目标函数的最终值:
Objective function value: 2.0000000268595803
算法终止消息:
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
最终点,在此示例中是
1 1
命令行窗口中显示每次迭代的算法信息:
Max Line search Directional First-order Iter F-count f(x) constraint steplength derivative optimality Procedure 0 3 10 2 Infeasible start point 1 6 4.84298 -0.1322 1 -5.22 1.74 2 9 4.0251 -0.01168 1 -4.39 4.08 Hessian modified twice 3 12 2.42704 -0.03214 1 -3.85 1.09 4 15 2.03615 -0.004728 1 -3.04 0.995 Hessian modified twice 5 18 2.00033 -5.596e-05 1 -2.82 0.0664 Hessian modified twice 6 21 2 -5.326e-09 1 -2.81 0.000522 Hessian modified twice Active inequalities (to within options.ConstraintTolerance = 1e-06): lower upper ineqlin ineqnonlin 3 4 Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.