matlab - Matlab优化工具箱中的非线性等式和不等式约束

标签 matlab optimization minimization

我需要优化以下功能:
f(x)= x ^ 2 + y ^ 3 + z ^ 4

有约束:
x + y + z = 10
1.5 + xy-z <= 0
xy> = -10

和限制:
-10 <= x <= 10
-5 <= y <= 5
0 <= z <= inf

我需要使用这些选项:
'LargeScale'='off','GradObj'='on','GradConstr'='on',我的代码如下所示:

options = optimset('LargeScale', 'off', 'GradObj','on','GradConstr','on');
A = [-1 0 0
  1 0 0
  0 -1 0
  0 1 0
  0 0 -1];
b = [-10 10 -5 5 0];
Aeq = [1 1 1];
beq = [10];
[x fval] = fmincon(@fun,[0 0 0],A, b, Aeq, beq,[],[],@constr, options);

function [y,g] = fun(x)
y = x(1).^2+x(2).^3+x(3).^4;
    if nargout > 1
        g = [2*x(1), 3*x(2).^2, 4*x(3).^3];
    end
end
function [c,ceq, GC, GCeq] = constr(x)
    c(1) = 1.5 + x(1)*x(2) - x(3);
    c(2) = -10 - x(1)*x(2);
    ceq = [];
    if nargout > 2
        GC = [x(2), -x(2);
              x(1), -x(1);
              0   ,    0];
        GCeq = [];
    end
end


预期结果是:
x = 10
y = -1
z = 0.05

能给我个建议吗?

最佳答案

您应用于x(1)x(2)的线性约束不正确。正如您所给出的,对x(1)起作用的两个约束表示为:

x(1) <= 10
-x(1) <= -10


满足这些要求的唯一值是x(1)=10。将两个RHS值都设置为10,您将对要实现的x(1)实施限制。

同样,您为第一个非线性约束提供的渐变不正确,相对于x(3),您缺少该渐变的-1值。

我在下面做了修改。运行此命令时,将得到[8.3084 0.0206 1.6710]的最佳解决方案。我认为您提供的预期结果不正确,它们不满足x + y + z = 10的相等约束

options = optimset('LargeScale', 'off', 'GradObj','on','GradConstr','on');
A = [-1 0 0
  1 0 0
  0 -1 0
  0 1 0
  0 0 -1];
b = [10 10 5 5 0];
Aeq = [1 1 1];
beq = [10];
[x fval] = fmincon(@fun,[0 0 0],A, b, Aeq, beq,[],[],@constr, options);

function [y,g] = fun(x)
y = x(1).^2+x(2).^3+x(3).^4;
    if nargout > 1
        g = [2*x(1), 3*x(2).^2, 4*x(3).^3];
    end
end
function [c,ceq, GC, GCeq] = constr(x)
c(1) = 1.5 + x(1)*x(2) - x(3);
c(2) = -10 - x(1)*x(2);
ceq = [];
if nargout > 2
    GC = [x(2), -x(2);
          x(1), -x(1);
          -1   ,    0];
    GCeq = [];
end

关于matlab - Matlab优化工具箱中的非线性等式和不等式约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53907978/

相关文章:

java - 改进 Java 代码 : too many if's

java - 关于用 Java 编写优化和执行代码的书籍

php - PDO检查数据库中是否存在300多个值的最佳方法

matlab - Simulink 中这个图标的含义是什么?

arrays - 使用之字形排序将 1d 矩阵转换为 2d 矩阵

matlab - R2016a : error constructing class when package & class name are equal

linux - Matlab:-maxNumCompThreads、超线程和 parpool

python - 如何在不使用梯度、不使用约束和范围的情况下最小化 Python 中的函数?

java - 最小化java中的复杂线性多变量函数

algorithm - 最小面积计算算法(仅在边缘放置瓷砖)