在命令窗口中输入
x0=[0.7,0.8,0.1,0.2];
options=optimset('largescale','on');
[x,fval]=fmincon(@eg,x0,[],[],[],[],[0.65,0.65,1e-1,1e-1],[0.95,0.95,0.3,0.3],[],options);
系统提示:FMINCON requires all values returned by user functions to be of data type double。
我的M文件如下:
function F = eg(x)
syms w r2 r3 e2 e3
x=[r2 r3 e2 e3];
u2=0.15;u3=0.15;
j1=0.0059;j2=u2*j1;j3=u3*j1;k1=j1*(100*pi)^2; k2=k1*(r2)^2*u2; k3=k1*(r3)^2*u3; c2=e2*2*(j2*k2)^0.5; c3=e3*2*(j3*k3)^0.5;
X=k1/(-j1*w^2+k1-((j2*w^2*(1i*w*c2+k2))/(-j2*w^2+1i*w*c2+k2)+ (j3*w^2*(1i*w*c3+k3))/(-j3*w^2+1i*w*c3+k3)));
f=abs(X);
x1=810*2*pi/60;
k=1;
h=5;
for j=1:100
w=x1;
x1=x1+h*(j-1);
x2=x1+h;
x3=x2+h;
fl=subs(f,w,x1); %极大值左边的一个数
fm=subs(f,w,x2); %极大值
fr=subs(f,w,x3); %极大值右边的一个数
if fm>=fl && fm>=fr %选择极大值
fmax(k)=fm;
x_opt(k)=x2;
k=k+1;
F=mean(fmax); %极大值的平均值
end
end
我认为可能与M文件中的syms变量有关。请各位仁兄多多指导啊! |