Debugging help needed: ODEs returning NaN

조회 수: 1 (최근 30일)
Patrick Young
Patrick Young 2011년 2월 17일
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