精品帖—matlab求解存在多个非线性不等式约束的多元约束优化问题方法

一、前言

这个matlab求解存在多个非线性不等式约束的多元约束优化问题方法真的很讨厌,经常看好多书和网页攻略也找不到合适的解法。最近看书,发现一个很有帮助的例题,同时结合自己在网上搜索的网友的解法,受到了一个启发性的解法,具体请看书中做的标记。如果还是不清楚,再看下第二个图后面的例题和回答。我想各位网友静下心来好好琢磨下这两个图片和后面那两个例题,聪明的你一定能搞定这个问题的!

(PS:有读者问这本书的名字,我就给出书的作者打个免费的广告吧,书名叫:“Matlab 2018 数学计算与工程分析-从入门到精通”,个人感觉这本书对于搞科研的小伙伴们来说非常好用,作者写的很用心,有好多实例非常实用,推荐大家看看。再推荐一本书:“”MATLAB数学建模经典案例实战 by 余胜威“”,这本书就是例题3中的例子,其实余胜威作者关于MATLAB相关的书都很好,我买过好几本推荐读者阅读。)

二、书中的解法

 三、网友的例子(为了让大家更加深刻理解书上第一张图标记的矩阵A和C1、C2,请看下面的例子)

例题1:用fmincon求目标函数最小值其中有多个非线性不等式约束,还有参数大于零的正整数约束,怎么在matlab的fmincon函数中表示这些约束条件?
 

主函数:
function f = fun(x)
ci = 10000;
cm = 1000000;
f = ((x(1)+x(2)+x(3))*ci + cm) / (x(1)*x(4)+x(2)*x(5)+x(3)*x(6));

约束条件
2000≤x(1)*x(4)≤8000;
600≤x(2)*x(5)≤2400;
200≤x(3)*x(6)≤800;
x(1)> x(2)> x(3)>0,且为正整数;
x(4)> x(5)> x(6)>0;

初值:x0=[3000, 1000, 400];

约束条件的函数怎么写?谢谢!

例题1回答:

x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
nonlcon就是非线性约束du可以zhi是函数可以是m文件,里面dao要有ce[],ceq[]
举个例子,写法版:
function [ce,ceq]=myfunc(x)
ce[1]=x1+sin(x2);%表示权x1+sin(x2)<=0的约束
ce[2]=x1-cos(x2);%表示x1-cos(x2)<=0的约束
ceq[1]=fun1;%表示fun1=0的约束
ceq[2]=fun2;%表示fun2=0的约束,这里fun1、fun2可以在其他M文件里提前写好。
%如果没有约束就直接ce[],ceq[]表示
求解的时候就直接
x = fmincon(@(x)myfun(x),x0,[],[],[],[],[],[],@(x) myfunc(x))%myfun(x)是提前写好的目标函数.

例题2

例题2的回答:

你的非线性等式约束函数是写准确了的,没问题,但是一定要单独保存成一个函数文件,如myfun.com,代码和你一样
function [c,ceq]=mycon(x)
c=[];
ceq(1)=x(3)^2/x(4)^2-(40-x(2)-x(1))/(x(1));
ceq(2)=1600-(40-x(2)-x(1))^2-(1+x(1)^2/x(4)^2)*x(3)^2;
ceq(3)=(x(3)^2+x(4)^2)*x(1)^3/3*x(4)^2-(40-x(2))*x(1)^2+(x(3)^2-2*40*x(2)+x(2)^2)*x(1)-x(2)^2*40-1/3*x(2)^3;
end
然后写一个脚本,或者直接在命令窗口输入以下代码,即可求解
fun=@(x)4*pi*40^2-2*pi*40*(x(1)+x(2))+2*pi*x(3)/x(4)*[x(1)/2*(x(1)^2+x(4)^2)^0.5+x(4)^2/2*log(x(1)+(x(1)^2+x(4)^2)^0.5)]-2*pi*x(3)*x(4)*log(x(4));
x0=[1 1 1 1];
A=[];
b=[];
Aeq=[];
beq=[];
lb=[];
ub=[];
[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,@mycon)
其中之一初始解和你给的不同,你的初始解是没有定义的,比如0怎么可以做分母呢,所有我随便改成了一个有定义的初始解x0 = [1 1 1 1];
最后结果为:

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.

<stopping criteria details>


x =

   16.0290   -8.7888    1.3007    0.9098


fval =

   1.9456e+04


exitflag =

     1


output =

  包含以下字段的 struct:

         iterations: 57
          funcCount: 515
    constrviolation: 3.0553e-11
           stepsize: 1.8349e-06
          algorithm: 'interior-point'
      firstorderopt: 3.9323e-04
       cgiterations: 30
            message: 'Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the default value of the optimality tolerance,↵and constraints are satisfied to within the default value of the constraint tolerance.↵↵Stopping criteria details:↵↵Optimization completed: The relative first-order optimality measure, 3.122226e-07,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 1.950213e-13, is less than options.ConstraintTolerance = 1.000000e-06.↵↵Optimization Metric                                            Options↵relative first-order optimality =   3.12e-07       OptimalityTolerance =   1e-06 (default)↵relative max(constraint violation) =   1.95e-13    ConstraintTolerance =   1e-06 (default)'


lambda =

  包含以下字段的 struct:

         eqlin: [0×1 double]
      eqnonlin: [3×1 double]
       ineqlin: [0×1 double]
         lower: [4×1 double]
         upper: [4×1 double]
    ineqnonlin: [0×1 double]


grad =

   1.0e+03 *

   -0.1071
   -0.2513
    0.8990
   -1.2595


hessian =

   1.0e+05 *

    0.0051    0.0021    0.0777   -0.1283
    0.0021    0.0011    0.0341   -0.0561
    0.0777    0.0341    1.2123   -1.9990
   -0.1283   -0.0561   -1.9990    3.2968

例题3

PS:各位读者看完后如果觉得对您有点用,请给我点个赞或者来个评论,或者来个打赏,鼓励下我继续写下去的信心,谢谢大家啦!

  • 38
    点赞
  • 142
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

舞动的白杨

客官,可否打赏UP主喝杯咖啡?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值