Debugging help needed: ODEs returning NaN

2 次查看(过去 30 天)
Hi! I'm trying to debug my code. I can't figure out what's causing my ODEs to return NaN. Y(:,2) and Y(:,3) have the initial condition for the first entry, and NaN for every other entry. I set the debugger to stop if NaN/Inf is encountered but I still can't figure out what the problem is. My code is as follows:
k1=0.05; %L/mol/min
k2=0.25; %L/mol/min
tbar=75; %min
cAo=0.6; %mol/L
cBo=0.4; %mol/L
Et=@(t)4*t*exp(-2*t/tbar)/tbar^2;
cCt=@(y)4/3.*(cAo-y(2))-2/3.*(cBo-y(3));
concs=@(t,y)[-Et(t);
-(k1*y(2)*y(3)+Et(t)/y(1)*(y(2)-cAo));
-(k1*y(2)*y(3)+k2*y(3)*cCt(y)+Et(t)/y(1)*(y(3)-cBo))];
[T,Y] = ode45(concs,[10*tbar 0],[0 1 0.343]);
Any help would be much appreciated. Thanks!

采纳的回答

Andrew Newell
Andrew Newell 2011-2-17
The short answer is that your problem is ill-conditioned, as you'll see from the messages if you use ode23s in place of ode45. You could try debugging this by putting the function CONCS in a separate file so you can set break points.
  2 个评论
Patrick Young
Patrick Young 2011-2-17
Thanks! The problem was that my initial condition for y(1) was 0 (which is it) but it was giving me a divide by 0 error :( So i made the initial condition 1e-8 instead and I got what I came for :)
Andrew Newell
Andrew Newell 2011-2-17
I'm glad to hear that the fix was that simple.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by