24小时热门版块排行榜    

查看: 3472  |  回复: 5

hustwzh

金虫 (小有名气)

[求助] 求大神帮忙解决一下,用MATLAB求解动力学数据总是出错~ 已有1人参与

如题,本人是个Matlab方面的小白,临时要用到,但自己纠结了好久还是没弄会,只好来向诸位大师求助了,代码及问题如下,多谢了
CODE:
function KineticsEst5 % 动力学ODE方程模型的参数估计
%
%
%
% The variables y here are y(1)=xB, y(2)=xoNB, y(3)=xmNB,y(4)=xpNB,y(5)=xDNB .
clear all
clc
k0 = [5 5 5 5 5]; % 参数初值
lb = [0 0 0 0 0]; % 参数下限
ub = [+inf +inf +inf +inf +inf]; % 参数上限
x0 = [0 0 0 0 0 0];
KineticsData;
yexp = ExpData(:,2:6); % yexp: 实验数据[x1 x4 x5 x6]
% 使用函数fmincon()进行参数估计
[k,fval,flag] = fmincon(@ObjFunc4Fmincon,k0,[],[],[],[],lb,ub,[],[],x0,yexp);
fprintf('\n使用函数fmincon()估计得到的参数值为:\n')
fprintf('\tk1 = %.4f\n',k(1))
fprintf('\tk2 = %.4f\n',k(2))
fprintf('\tk3 = %.4f\n',k(3))
fprintf('\tk4 = %.4f\n',k(4))
fprintf('\tk5 = %.4f\n',k(5))
fprintf(' The sum of the squares is: %.1e\n\n',fval)
k_fmincon = k;
% 使用函数lsqnonlin()进行参数估计
[k,resnorm,residual,exitflag,output,lambda,jacobian] = ...
      lsqnonlin(@ObjFunc4LNL,k0,lb,ub,[],x0,yexp);
ci = nlparci(k,residual,jacobian);
fprintf('\n\n使用函数lsqnonlin()估计得到的参数值为:\n')
Output
% 以函数fmincon()估计得到的结果为初值,使用函数lsqnonlin()进行参数估计
k0 = k_fmincon;
[k,resnorm,residual,exitflag,output,lambda,jacobian] = ...
    lsqnonlin(@ObjFunc4LNL,k0,lb,ub,[],x0,yexp);
ci = nlparci(k,residual,jacobian);
fprintf('\n\n以fmincon()的结果为初值,使用函数lsqnonlin()估计得到的参数值为:\n')
Output

% ------------------------------------------------------------------
function f = ObjFunc4Fmincon(k,x0,yexp)
tspan = [0.00 : 5.03 : 15.09];
[t x] = ode45(@KineticEqs,tspan,x0,[],k);
y(:,1) = x(:,1);
y(:,2:4) = x(:,4:6);
f = sum((y(:,1)-yexp(:,1)).^2) + sum((y(:,2)-yexp(:,2)).^2) ...
     + sum((y(:,3)-yexp(:,3)).^2) + sum((y(:,4)-yexp(:,4)).^2);
% ------------------------------------------------------------------
function f = ObjFunc4LNL(k,x0,yexp)
tspan = [0.00 : 5.03 : 15.09];
[t x] = ode45(@KineticEqs,tspan,x0,[],k);
y(:,1) = x(:,1);
y(:,2:4) = x(:,4:6);
f1 = y(:,1) - yexp(:,1);
f2 = y(:,2) - yexp(:,2);
f3 = y(:,3) - yexp(:,3);
f4 = y(:,4) - yexp(:,4);
f = [f1; f2; f3; f4];
% ------------------------------------------------------------------
function dxdt = KineticEqs(t,x,k)
M=1.003;
dxdt = ...
   [ ( (k(1)+k(2)+k(3))*(1-x(1))*M*(1-x(2)-x(3)-x(4)-2*x(5)) )
     ( M*(1-x(2)-x(3)-x(4)-2*x(5))*(k(1)*(1-x(1))-k(4)*x(2)) )
     ( k(2)*(1-x(1))*M*(1-x(2)-x(3)-x(4)-2*x(5)) )
     ( M*(1-x(2)-x(3)-x(4)-2*x(5))*(k(3)*(1-x(1))-k(5)*x(4)) )
     ( M*(1-x(2)-x(3)-x(4)-2*x(5))*(k(4)*x(2)+k(5)*x(4)) )
];

错误使用 odearguments (line 92)
KINETICEQS 返回的矢量的长度为 5,但初始条件矢量的长度为 6。KINETICEQS 返回的矢量和初始条件矢量的元素数目必须相同。

出错 ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

出错 KineticsEst>ObjFunc4Fmincon (line 48)
[t x] = ode45(@KineticEqs,tspan,x0,[],k);

出错 fmincon (line 564)
      initVals.f = feval(funfcn{3},X,varargin{:});

出错 KineticsEst (line 19)
[k,fval,flag] = fmincon(@ObjFunc4Fmincon,k0,[],[],[],[],lb,ub,[],[],x0,yexp);

原因:
    Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
回复此楼

» 本帖附件资源列表

  • 欢迎监督和反馈:小木虫仅提供交流平台,不对该内容负责。
    本内容由用户自主发布,如果其内容涉及到知识产权问题,其责任在于用户本人,如对版权有异议,请联系邮箱:xiaomuchong@tal.com
  • 附件 1 : KineticsData.m
  • 2017-02-18 14:07:42, 498 bytes

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

chendequan

铁虫 (小有名气)

【答案】应助回帖

感谢参与,应助指数 +1
>> KineticsEst5

Local minimum possible. Constraints satisfied.

fmincon stopped because the size of the current step is less than
the default value of the step size tolerance and constraints are
satisfied to within the default value of the constraint tolerance.

<stopping criteria details>


使用函数fmincon()估计得到的参数值为:
        k1 = 0.0358
        k2 = 0.0021
        k3 = 5.6484
        k4 = 22.7533
        k5 = 14.7665
The sum of the squares is: 1.5e+00


Local minimum possible.

lsqnonlin stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.

<stopping criteria details>



使用函数lsqnonlin()估计得到的参数值为:
        k1 = 0.8360
        k2 = 0.0128
        k3 = 3.0166
        k4 = 38.9765
        k5 = 7.6220

Local minimum possible.

lsqnonlin stopped because the size of the current step is less than
the default value of the step size tolerance.

<stopping criteria details>



以fmincon()的结果为初值,使用函数lsqnonlin()估计得到的参数值为:
        k1 = 0.0358
        k2 = 0.0021
        k3 = 5.6484
        k4 = 22.7533
        k5 = 14.7665
QQ:516477448,真心帮助解决MATLAB相关问题,提供详细资料,Word文档明确具体问题及要求,尽力而为!
2楼2017-02-19 10:02:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hustwzh

金虫 (小有名气)

你好,你可以把修改后的程序发给我看一下吗?
3楼2017-02-19 15:44:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hustwzh

金虫 (小有名气)

引用回帖:
2楼: Originally posted by chendequan at 2017-02-19 10:02:37
>> KineticsEst5

Local minimum possible. Constraints satisfied.

fmincon stopped because the size of the current step is less than
the default value of the step size tolerance and constrai ...

你好,你可以把修改后的程序发给我看一下吗?
4楼2017-02-19 15:45:13
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

hustwzh

金虫 (小有名气)

引用回帖:
2楼: Originally posted by chendequan at 2017-02-19 10:02:37
>> KineticsEst5

Local minimum possible. Constraints satisfied.

fmincon stopped because the size of the current step is less than
the default value of the step size tolerance and constrai ...

你好,你可以把修改后的程序发给我看一下吗?
5楼2017-02-19 15:45:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

chendequan

铁虫 (小有名气)

【答案】应助回帖

内容已删除
QQ:516477448,真心帮助解决MATLAB相关问题,提供详细资料,Word文档明确具体问题及要求,尽力而为!
6楼2017-02-19 19:55:58
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 hustwzh 的主题更新
信息提示
请填处理意见