作业帮 > 综合 > 作业

关于用MATLAB优化函数fmincon进行约束优化编程的问题

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/04/25 00:55:08
关于用MATLAB优化函数fmincon进行约束优化编程的问题
我的模型
Min
s.t.
%编写约束条件M文件(yueshu.m).
function [c,ceq]=yueshufun(x1,x2,x3)
c=[(x2^2+x3^2-(1-x1)^2)/2*x2*x3-sqrt(3)/2;
sqrt(2)/2-(x2^2+x3^2-(1-x1)^2)/2*x2*x3];
ceq=[];
目标函数goal_func.m:
function H=goal_func(x1,x2,x3,x4)
F=0;
for i=1:20;
%w表示输入角θ.输入角变化180度,分20次,每次变化9度.
wi=1*pi*i./20;
G=(atan(x1*sin(x4)/(1-x1*cos(x4)))+acos((x1^2-x2^2+x3^2+1-2*x1*cos(x4))...
/(2*x3*sqrt(x1^2+1-2*x1*cos(x4))))-atan((x1*sin(x4+wi))/(1-x1*cos(x4+wi)))...
-acos((x1^2-x2^2+x3^2+1-2* x1*cos(x4+wi))/(2*x3*sqrt(x1^2+1-2*x1*cos(x4+wi))))-6*sin(pi*wi/180)/pi)^2;
F=F+G;
end %运算之后是数学模型中的目标函数.
H=F-r*(log(-x1-x2+x3+1)+log(-x1+x2-x3+1)+log(-x1+x2+x3-1)+log(x1-0.1)+log(-x1+0.5)...
+log(x2-0.1)+log(-x2+1.4)+log(x3-0.1)+log(-x3+0.9)...
+log(x2^2+x3^2-(1-x1)^2)/2*x2*x3-sqrt(3)/2+log(sqrt(2)/2-(x2^2+x3^2-(1-x1)^2)/2*x2*x3)); %这是惩罚函数的目标函数.惩罚函数法是解决约束优化问题的一种思路,是将目标函数和约束条件按照规定构造成新的目标函数,将约束优化问题转化为无约束优化问题
下面是fmincon函数的调用:
%调用fmincon函数进行求解.
x0=[0.3,1,0.6,2*pi/3];
lb=[];
ub=[];
options=optimset('Largescale','on','display','iter','tolx',1e-4); %搞不懂这里options干什么的.
for a=1:100;
b=10^a;
r=1/b;
[x,fval,exitflag,output,lambda]=fmincon('goal_func',x0,A,b,[],[],[],[],'yueshufun',options);
if fval>=0.001 %收敛精度为0.01.
break;
end
end
最后是几个已知参数:
下面是参数A:9*3的
1\x051\x05-1
1\x05-1\x051
1\x05-1\x05-1
-1\x050\x050
1\x050\x050
0\x05-1\x050
0\x051\x050
0\x050\x05-1
0\x050\x051
下面是b
1
1
-1
-0.100000000000000
0.500000000000000
-0.100000000000000
1.40000000000000
-0.100000000000000
0.900000000000000
最后还有初始值x0
0.300000000000000\x051\x050.600000000000000\x052.09439510239320
最后是运行是出现的问题::
dyfmincon
Warning:Trust-region-reflective method does not currently solve this type of problem,
using active-set (line search) instead.
> In fmincon at 439
In dyfmincon at 9
Input argument "x4" is undefined.
Error in ==> goal_func at 6
G=(atan(x1*sin(x4)/(1-x1*cos(x4)))+acos((x1^2-x2^2+x3^2+1-2*x1*cos(x4))...
Error in ==> fmincon at 519
initVals.f = feval(funfcn{3},X,varargin{:});
Error in ==> dyfmincon at 9
[x,fval,exitflag,output,lambda]=fmincon('goal_func',x0,A,b,[],[],[],[],'yueshufun',options);
Caused by:
Failure in initial user-supplied objective function evaluation.FMINCON cannot continue.
1 function [c,ceq]=yueshufun(x)
x1=x(1);x2=x(2);x3=x(3);
c=[(x2^2+x3^2-(1-x1)^2)/2*x2*x3-sqrt(3)/2;
sqrt(2)/2-(x2^2+x3^2-(1-x1)^2)/2*x2*x3];
ceq=[]; %%非线性约束关系函数的输入是一个矩阵
2 function H=goal_func(x)
x1=x(1);x2=x(2);x3=x(3);x4=x(4);
F=0;
for i=1:20;
%w表示输入角θ.输入角变化180度,分20次,每次变化9度.
wi=1*pi*i./20;
G=(atan(x1*sin(x4)/(1-x1*cos(x4)))+acos((x1^2-x2^2+x3^2+1-2*x1*cos(x4))...
/(2*x3*sqrt(x1^2+1-2*x1*cos(x4))))-atan((x1*sin(x4+wi))/(1-x1*cos(x4+wi)))...
-acos((x1^2-x2^2+x3^2+1-2* x1*cos(x4+wi))/(2*x3*sqrt(x1^2+1-2*x1*cos(x4+wi))))-6*sin(pi*wi/180)/pi)^2;
F=F+G;
end %运算之后是数学模型中的目标函数.
H=F-r*(log(-x1-x2+x3+1)+log(-x1+x2-x3+1)+log(-x1+x2+x3-1)+log(x1-0.1)+log(-x1+0.5)...
+log(x2-0.1)+log(-x2+1.4)+log(x3-0.1)+log(-x3+0.9)...
+log(x2^2+x3^2-(1-x1)^2)/2*x2*x3-sqrt(3)/2+log(sqrt(2)/2-(x2^2+x3^2-(1-x1)^2)/2*x2*x3)); %%目标函数中r没有定义,在目标函数中定义r.
3 A中,楼主要求的参数是3个,但是G中用到了x4,那你的x中就必须有四个值,而不是三个值;
4 x0=[0.3;1;0.6;2*pi/3;];初始值x0应该是列向量
5 options=optimset('Largescale','on','display','iter','tolx',1e-4); 是用来控制fmincon的各种参数,tolx是优化终止条件;LargeScale是采用的算法;display选择iter显示迭代次数;
6 注意log(a)a不能为0,我测试了下,提示log运算中a出现0的情况;