Effacer les filtres
Effacer les filtres

Debugging help needed: ODEs returning NaN

2 vues (au cours des 30 derniers jours)
Patrick Young
Patrick Young le 17 Fév 2011
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!

Réponse acceptée

Andrew Newell
Andrew Newell le 17 Fév 2011
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 commentaires
Patrick Young
Patrick Young le 17 Fév 2011
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 le 17 Fév 2011
I'm glad to hear that the fix was that simple.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by