MATLAB进行fmincon函数优化时非线性不等式约束总是报错。
Show older comments
%1-函数文件objfun.m
function f=objfun(x)
a=10;
d=2*(x(1).*cos(x(5)*pi/180)-x(2).*cos(x(4)*pi/180));
s=2*x(1).*cos(x(5)*pi/180);
b=cos((x(4)-a)*pi/180);
c=d./2+x(2).*b;
e=c./x(1);
t=2*x(1).*e;
g=t-s+0.0001*(x(3)+x(6));
f=-g;
end
%2-非线性约束函数文件confun.m
function [c,ceq]=confun(x)
c=[x(2).*cos(x(4)*pi/180)-x(1).*cos(x(5)*pi/180);(x(1).*sin(x(5)*pi/180)+x(2).*sin(x(54)*pi/180))./(cos(x(6)*pi/180)+x(3).*sin(x(6)*pi/180));(2*x(1).*cos(x(5)*pi/180))./(cos(x(6)*pi/180))-100;2*x(1).*cos(x(5)*pi/180)-2*x(2).*cos(x(4)*pi/180)-20;15-2*x(1).*cos(x(5)*pi/180)+2*x(2).*cos(x(4)*pi/180)];
%5个不等式非线性约束
ceq=[];
end
%3-优化函数应用
x0=[30 30 13 57 34 50];
%经过核对,初值全部符合原不等式非线性约束
A=[0 -1 1 0 0 0;
-1 0 1 0 0 0;
-1 1 0 0 0 0;
0 0 0 -1 0 0];
b=[0 0 0 -10];
Aeq=[];beq=[];
lb=[20,20,10,10,10,20];
ub=[60,50,20,70,70,70];
options=optimset('largescale','off','display','iter');
[xopt,fval,exitflag,output]=fmincon(@objfun,x0,A,b,Aeq,beq,lb,ub,@confun,options)
出来的结果是这样
>> Untitled3
索引超出矩阵维度。
出错 confun (line 6)
c=[x(2).*cos(x(4)*pi/180)-x(1).*cos(x(5)*pi/180);(x(1).*sin(x(5)*pi/180)+x(2).*sin(x(54)*pi/180))./(cos(x(6)*pi/180)+x(3).*sin(x(6)*pi/180));(2*x(1).*cos(x(5)*pi/180))./(cos(x(6)*pi/180))-100;2*x(1).*cos(x(5)*pi/180)-2*x(2).*cos(x(4)*pi/180)-20;15-2*x(1).*cos(x(5)*pi/180)+2*x(2).*cos(x(4)*pi/180)];
出错 fmincon (line 623)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
出错 Untitled3 (line 13)
[xopt,fval,exitflag,output]=fmincon(@objfun,x0,A,b,Aeq,beq,lb,ub,@confun,options)
原因:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
就是感觉这个约束咋有毛病哦~
Answers (1)
真人百家家乐下载【www.xbs3512.com】
on 14 Oct 2022
0 votes
这只能说语法没有问题了,至于逻辑问题,你再好好看看。
另外fmincon是局部最有解,也可能在别的地方的解满足你的要求。事实是,confun(xopt)<0,满足约束的。
哈哈....,你的代码里面竟然有个x(54)? 是x(5)还是x(4)
Categories
Find more on Dynamic System Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!