该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
function[ x,minf]=minconSimpSearch2(f,g,gg,X,alpha,sita,gama,beta,var,eps)
% f:目标函数
% g:约束函数1 gg:约束函数2
% X:初始复合形
% alpha:反射系数
% sita:紧缩系数
% gama:扩张系数
% beta:收缩系数
% var:自变量向量
% eps:精度
% x:目标函数取最小值时的自变量
% minf:目标函数的最小值
N=size(X);
n=N(2);
FX=zeros(1,n);
while 1
for i=1:n
FX(i)=subs(f,var,X(:,i));
end
[XS,IX]=sort(FX);
Xsorted=X(:,IX); %按照IX的顺序重新排列X
px=sum(Xsorted(:,1:(n-1)),2)/(n-1); %sum(a,2),a矩阵行相加 Xsorted(:,1:2)保留Xsort的1,2列。中心点坐标。
Fpx=subs(f,var,px); %中心点函数值
SumF=0
for i=1:n
SumF=SumF+(FX(IX(i))-Fpx)^2; %判断收敛
end
SumF=sqrt(SumF/(n-1));
if SumF<=eps
x=Xsorted(:,1);
break;
else
bcon_1=1;
cof_alpha=alpha;
while bcon_1
x2=px+cof_alpha*(px-Xsorted(:,n)); %算反射点的坐标
gx2=subs(g,var,x2); %看有没有出界
ggx2=subs(gg,var,x2);
if gx2>=0 ggx2>=0 2<=x2(1)<=4 0.5<=x2(2)<=1
bcon_1=0;
else
cof_alpha=0.7*(cof_alpha);
end
end
fx2=subs(f,var,x2); %反射点函数值
if fx2
cof_gama=gama;
bcon_2=1;
while bcon_2
x3=x2+cof_gama*(x2-px); %扩张步骤,感觉应该用x2代贴第一部分px
gx3=subs(g,var,x3);
ggx3=subs(gg,var,x3);
fx3=subs(f,var,x3);
if gx3>=0 ggx3>=0 2<=x3(1)<=4 0.5<=x3(2)<=1
bcon_2=0;
if fx3
count=1;
else
count=2;
end
else
bcon_2=0;
count=3;
end
end
if count==1
Xsorted(:,n)=x3;
X=Xsorted;
continue
else
Xsorted(:,n)=x2;
X=Xsorted;
continue
end
else
if fx2
Xsorted(:,n)=x2;
X=Xsorted;
continue
else
if fx2
Xsorted(:,n)=x2;
cof_beta=beta;
bcon_3=1;
while bcon_3<4
%x4=px+cof_beta*(Xsorted(:,n)-px)
x4=Xsorted(:,n)+cof_beta*(px-Xsorted(:,n));
gx4=subs(g,var,x4);
ggx4=subs(gg,var,x4);
if gx4>=0 ggx4>=0 2<=x4(1)<=4 0.5<=x4(2)<=1
bcon_3=5;;
else
cof_beta=cof_beta/2;
bcon_3=bcon_3+1;
end
end
if gx4>=0 ggx4>=0 2<=x4(1)<=4 0.5<=x4(2)<=1
fx4=subs(f,var,x4);
FNnew=subs(f,var,Xsorted(:,n));
if fx4
Xsorted(:,n)=x4;
X=Xsorted;
continue
else
x0=Xsorted(:,1);
for i=1:n
Xsorted(:,i)=x0+sita*(Xsorted(:,i)-x0);
end
end
else
x0=Xsorted(:,1);
for i=1:n
Xsorted(:,i)=x0+sita*(Xsorted(:,i)-x0);
X=Xsorted;
continue
end
end
else
x0=Xsorted(:,1);
for i=1:n
Xsorted(:,i)=x0+sita*(Xsorted(:,i)-x0);
X=Xsorted;
continue
end
end
end
end
end
X=Xsorted;
end
minf=subs(f,var,x);
disp(x);
disp(f);
用复合形法求约束问题的最优解,目标函数:
f(x)=25/(x1x2^3)
约束条件:
g2(x)=1-0.4x1x2≥0
2≤x1≤4
0.5≤x2≤1