我用MATLAB编程,由最小二乘法求函数最小值,但就是运行不出来,不知道是哪里出错了,请帮我改正一下
functionf=func(a)x1=[0.2760.4010.49580.579];%苯的液相组成的实验值x2=1-x1;%正庚烷的液相组成的实验值ye=[0.393...
function f = func( a )
x1=[0.276 0.401 0.4958 0.579]; %苯的液相组成的实验值
x2=1-x1; %正庚烷的液相组成的实验值
ye=[0.393 0.51 0.591 0.67]; %苯的气相组成的实验值
yr=1-ye; %正庚烷的气相组成的实验值
A=[6.87987 6.89386];B=[1196.76 1264.37];C=[219.161 216.64];
P=760; %大气压
tz=[89.5 87.15 85.1 83.65]; %主温度计的数值
ts=[29.5 28.1 27.9 26.9]; %辅助温度计的数值
tg=0.00016*60.*(tz-ts);tx=[0 0 0 0]; %温度校正值,温度修正值
t=tz+tg+tx; %实际温度
f=0;
for i=1:4
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
p2(i)=10^(A(2)-B(2)/(C(2)+t(i)));
end
for j=1:4
r1(j)=exp(-log(x1(j)+a(1)*x2(j))+x2(j)*(a(1)/(x1(j)+a(1)*x2(j))-a(2)/(x2(j)+a(2)*x1(j))));
r2(j)=exp(-log(x2(j)+a(2)*x1(j))+x1(j)*(a(2)/(x2(j)+a(2)*x1(j))-a(1)/(x1(j)+a(1)*x2(j))));
y1(j)=r1(j)*x1(j)*p1(j)/P;
y2(j)=r2(j)*x2(j)*p2(j)/P;
f=(ye(j)-y1(j))^2+(yr(j)-y2(j))^2+f;
end
运行结果如下:
a0=[1,1];
>> x=lsqnonlin('func',a0)
y1 =
[] [] [] []
??? Conversion to cell from double is not possible.
Error in ==> func at 14
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
Error in ==> lsqnonlin at 203
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue. 展开
x1=[0.276 0.401 0.4958 0.579]; %苯的液相组成的实验值
x2=1-x1; %正庚烷的液相组成的实验值
ye=[0.393 0.51 0.591 0.67]; %苯的气相组成的实验值
yr=1-ye; %正庚烷的气相组成的实验值
A=[6.87987 6.89386];B=[1196.76 1264.37];C=[219.161 216.64];
P=760; %大气压
tz=[89.5 87.15 85.1 83.65]; %主温度计的数值
ts=[29.5 28.1 27.9 26.9]; %辅助温度计的数值
tg=0.00016*60.*(tz-ts);tx=[0 0 0 0]; %温度校正值,温度修正值
t=tz+tg+tx; %实际温度
f=0;
for i=1:4
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
p2(i)=10^(A(2)-B(2)/(C(2)+t(i)));
end
for j=1:4
r1(j)=exp(-log(x1(j)+a(1)*x2(j))+x2(j)*(a(1)/(x1(j)+a(1)*x2(j))-a(2)/(x2(j)+a(2)*x1(j))));
r2(j)=exp(-log(x2(j)+a(2)*x1(j))+x1(j)*(a(2)/(x2(j)+a(2)*x1(j))-a(1)/(x1(j)+a(1)*x2(j))));
y1(j)=r1(j)*x1(j)*p1(j)/P;
y2(j)=r2(j)*x2(j)*p2(j)/P;
f=(ye(j)-y1(j))^2+(yr(j)-y2(j))^2+f;
end
运行结果如下:
a0=[1,1];
>> x=lsqnonlin('func',a0)
y1 =
[] [] [] []
??? Conversion to cell from double is not possible.
Error in ==> func at 14
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
Error in ==> lsqnonlin at 203
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue. 展开
2个回答
展开全部
好像没有什么问题,是不是你的Matlab版本太低?
运行情况:
>> a0=[1,1];
>> x=lsqnonlin('func',a0)
Warning: Large-scale method requires at least as many equations as variables;
switching to line-search method instead. Upper and lower bounds will be ignored.
> In optim\private\lsqncommon at 169
In lsqnonlin at 147
Optimization terminated: directional derivative along
search direction less than TolFun and infinity-norm of
gradient less than 10*(TolFun+TolX).
x =
0.131269887748105 2.01369250329103
运行情况:
>> a0=[1,1];
>> x=lsqnonlin('func',a0)
Warning: Large-scale method requires at least as many equations as variables;
switching to line-search method instead. Upper and lower bounds will be ignored.
> In optim\private\lsqncommon at 169
In lsqnonlin at 147
Optimization terminated: directional derivative along
search direction less than TolFun and infinity-norm of
gradient less than 10*(TolFun+TolX).
x =
0.131269887748105 2.01369250329103
追问
奇怪,我的是R2010b,我怎么就运行出来两个负数,而且也是提醒错误呢
追答
我用的R14版本比R2010b还低,好像没有道理啊。是不是软件上其他设置上有啥问题?
展开全部
function f = func( a )
x1=[0.276 0.401 0.4958 0.579]; %苯的液相组成的实验值
x2=1-x1; %正庚烷的液相组成的实验值
ye=[0.393 0.51 0.591 0.67]; %苯的气相组成的实验值
yr=1-ye; %正庚烷的气相组成的实验值
A=[6.87987 6.89386];B=[1196.76 1264.37];C=[219.161 216.64];
P=760; %大气压
tz=[89.5 87.15 85.1 83.65]; %主温度计的数值
ts=[29.5 28.1 27.9 26.9]; %辅助温度计的数值
tg=0.00016*60.*(tz-ts);tx=[0 0 0 0]; %温度校正值,温度修正值
t=tz+tg+tx; %实际温度
f=0;
for i=1:4
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
p2(i)=10^(A(2)-B(2)/(C(2)+t(i)));
end
for j=1:4
r1(j)=exp(-log(x1(j)+a(1)*x2(j))+x2(j)*(a(1)/(x1(j)+a(1)*x2(j))-a(2)/(x2(j)+a(2)*x1(j))));
r2(j)=exp(-log(x2(j)+a(2)*x1(j))+x1(j)*(a(2)/(x2(j)+a(2)*x1(j))-a(1)/(x1(j)+a(1)*x2(j))));
y1(j)=r1(j)*x1(j)*p1(j)/P;
y2(j)=r2(j)*x2(j)*p2(j)/P;
f=(ye(j)-y1(j))^2+(yr(j)-y2(j))^2+f;
end
运行结果如下:
a0=[1,1];
>> x=lsqnonlin('func',a0)
y1 =
[] [] [] []
??? Conversion to cell from double is not possible.
Error in ==> func at 14
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
Error in ==> lsqnonlin at 203
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
x1=[0.276 0.401 0.4958 0.579]; %苯的液相组成的实验值
x2=1-x1; %正庚烷的液相组成的实验值
ye=[0.393 0.51 0.591 0.67]; %苯的气相组成的实验值
yr=1-ye; %正庚烷的气相组成的实验值
A=[6.87987 6.89386];B=[1196.76 1264.37];C=[219.161 216.64];
P=760; %大气压
tz=[89.5 87.15 85.1 83.65]; %主温度计的数值
ts=[29.5 28.1 27.9 26.9]; %辅助温度计的数值
tg=0.00016*60.*(tz-ts);tx=[0 0 0 0]; %温度校正值,温度修正值
t=tz+tg+tx; %实际温度
f=0;
for i=1:4
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
p2(i)=10^(A(2)-B(2)/(C(2)+t(i)));
end
for j=1:4
r1(j)=exp(-log(x1(j)+a(1)*x2(j))+x2(j)*(a(1)/(x1(j)+a(1)*x2(j))-a(2)/(x2(j)+a(2)*x1(j))));
r2(j)=exp(-log(x2(j)+a(2)*x1(j))+x1(j)*(a(2)/(x2(j)+a(2)*x1(j))-a(1)/(x1(j)+a(1)*x2(j))));
y1(j)=r1(j)*x1(j)*p1(j)/P;
y2(j)=r2(j)*x2(j)*p2(j)/P;
f=(ye(j)-y1(j))^2+(yr(j)-y2(j))^2+f;
end
运行结果如下:
a0=[1,1];
>> x=lsqnonlin('func',a0)
y1 =
[] [] [] []
??? Conversion to cell from double is not possible.
Error in ==> func at 14
p1(i)=10^(A(1)-B(1)/(C(1)+t(i)));
Error in ==> lsqnonlin at 203
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQNONLIN cannot continue.
追问
你干嘛把我的问题又贴一遍啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询