我在用fmincon 的时候遇到了“输入参数不足”的问题,求各位帮忙看看,这是我的代码:
x0=[1,-1,1,-1,1,1];
A=[ -0.0206 -0.0401 -0.1897 -0.2006 -0.0302 -0.0787;
-1 0 0 0 0 0;
0 0 1 1 0 0;
0 0 0 0 1 0;
0 0 0 0 0 -1];
b=[-0.07;-0.1;0.3;0.3;0.15];
Aeq=[1 1 1 1 1 1];
beq=[1];
VLB=[0;0;0;0;0;0];
VUB=[1;1;1;1;1;1];
[x,fval]=fmincon(@fun4,x0,A,b,Aeq,beq,VLB,VUB,@mycon)
出错原因:
输入参数的数目不足。
出错 fmincon
第12行(最后一行)
代码不完整,fun4和mycon在哪里?建议参考fmincon官方文档的语法格式写:https://ww2.mathworks.cn/help/optim/ug/fmincon.html?s_tid=doc_ta
如果对题主有帮助,麻烦点击采纳,谢谢
把fun4函数和mycon函数用两个脚本写,不要放在一起,且脚本名字就是fun4和mycon
代码没有问题,可以求解
fun4没有定义上,是不是代码没传完整,mycon也没有定义,是不是可以删掉?
麻烦大家再看一下,这是后面的代码:
function f=fun4(x)
%f=x(1)*r1+x(2)*r2+x(3)*r3+x(4)*r4+x(5)*r5+x(6)*r6;
f=-(0.0206*x(1)+0.0401*x(2)+0.1897*x(3)+0.2006*x(4)+0.0302*x(5)+0.0787*x(6));
end
function [g,cep]=mycon(x)
g=(x(5)*((1463*x(3))/100000 - (9261*x(2))/2500000 + (891*x(4))/125000 + x(5)/25 + (1487*x(6))/200000) ...
+ x(6)*((62909*x(3))/1600000 - (346773*x(2))/100000000 + (38313*x(4))/2000000 + (1487*x(5))/200000 + (41711*x(6))/800000) ...
+ x(3)*((17689*x(3))/160000 - (957999*x(2))/200000000 + (10773*x(4))/200000 + (1463*x(5))/100000 + (62909*x(6))/1600000) ...
+ x(4)*((10773*x(3))/200000 - (583443*x(2))/250000000 + (6561*x(4))/250000 + (891*x(5))/125000 + (38313*x(6))/2000000) ...
- x(2)*((957999*x(3))/200000000 - (1058841*x(2))/100000000 + (583443*x(4))/250000000 + (9261*x(5))/2500000 + (346773*x(6))/100000000)-0.0777);
cep=(x(1)+x(2)+x(3)+x(4)+x(5)+x(6)-1);
end
https://blog.csdn.net/m0_48038938/article/details/121254302
这里详细得写了,希望对你有所帮助