Borrar filtros
Borrar filtros

Debugging help needed: ODEs returning NaN

4 visualizaciones (últimos 30 días)
Patrick Young
Patrick Young el 17 de Feb. de 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!

Respuesta aceptada

Andrew Newell
Andrew Newell el 17 de Feb. de 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 comentarios
Patrick Young
Patrick Young el 17 de Feb. de 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 el 17 de Feb. de 2011
I'm glad to hear that the fix was that simple.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by