是啊,怎么回事啊!我也经常出现这些问题,请各位高手帮帮忙了。
function f=myfun(x) 
%M汽车满载时前轴利用附着系数
M=x(2)*x(1)*L/(L-A+x(1)*H);
%N汽车满载时后轴利用附着系数
N=(1-x(2))*x(1)*L/(A-x(1)*H);
%m汽车空载时前轴利用附着系数
m=x(2)*x(1)*L/(L-a+x(1)*h);
%n汽车空载时后轴利用附着系数
n=(1-x(2))*x(1)*L/(a-x(1)*h);
%目标函数
f=w*[(M-x(1))^2+(N-x(1))^2]+(1-w)*[(m-x(1))^2+(n-x(1))^2];

我保存为myfun0.m文件了

function[c,ceq]=mycon(x) 
M=x(2)*x(1)*L/(L-A+x(1)*H);
N=(1-x(2))*x(1)*L/(A-x(1)*H);
m=x(2)*x(1)*L/(L-a+x(1)*h);
n=(1-x(2))*x(1)*L/(a-x(1)*h);
%c(i)(i=1,2,…10)非线性不等式约束
if(x(1)>=0.15&x(1)<=0.3)
 c(1)=M-x(1)-0.08;
 c(2)=m-x(1)-0.08;
 c(3)=x(1)-M-0.08;
 c(4)=x(1)-m-0.08;
 c(5)=N-x(1)-0.08;
 c(6)=n-x(1)-0.08;
 c(7)=x(1)-N-0.08;
 c(8)=x(1)-n-0.08;
 elseif(x(1)>=0.3&x(1)<=0.8)
 c(9)=0.74*N-x(1)+0.0188;
 c(10)=0.74*n-x(1)+0.0188;
 end
%ceq非线性等式约束 
ceq=[];

我保存为mycon0.m文件了,都是在work同一个目录下.
然后调用

>> H=1170;A=2950;h=845;a=2100;L=3950;w=0.5;
>> x0=[0.15,0];
>> lb=[0.15,0];ub=[0.8,1];
>> [x,fval]=fmincon('myfun0',x0,[],[],[],[],lb,ub,'mycon0')
Warning: Large-scale (trust region) method does not currently solve this type of problem,
 switching to medium-scale (line search).
> In fmincon at 271
??? Error using ==> fmincon
FMINCON cannot continue because user supplied objective function failed with the following error:
Undefined function or variable 'L'.
[color=FF0000]我也不知道哪里出现错误了[/color]