0
$\begingroup$

I'm trying to solve a set of differential equations in which one of the functions that describe the time derivative gets values which make it divide by zero

x'[t] = (Exp[x] - 1)/(Exp[x] - 1 + x) 

So what happens is that when NDSolve gets values of x=0 you get that an Infinite expression of 1/0 encountered.

However, when I have x=0 I would actually like to replace it with the limit of x->0

 Limit[(Exp[x] - 1)/(Exp[x] - 1 + x), x -> 0]

which is 1/2.

Any suggestions of how to implement the idea in NDSolve?

Addition

Look for simplicity at the following case

NDSolve[{x'[t] == (Exp[x[t]] - 1)/(Exp[x[t]] - 1 + x[t]), 
  x[0] == 0}, x, {t, 0, 1}]

Here x'[t] encounters 1/0 in the initial condition, but I would like it to get the limit of x->0 which is 1/2. Note that in my problem which is far more complicated, x'[t] encounters this limit many times and the value of the limit is varied with respect to other state variables, therefore I would like the limit to be calculated in each iteration.

$\endgroup$
  • $\begingroup$ Does your real problem involve 1/0 in the initial conditions or elsewhere? I find that your simple example runs fine as long as you don't use x[0] == 0. $\endgroup$ – Chris K May 2 at 15:31
  • $\begingroup$ @ChrisK, elsewhere, actually all along the solution periodically. I gave the initial condition as an example. $\endgroup$ – jarhead May 2 at 15:49
  • $\begingroup$ Could you give an example where it doesn't work x[0] != 0? $\endgroup$ – Chris K May 2 at 16:14
4
$\begingroup$

If:

eq = With[{x = x[t]}, D[x, t] == If[x == 0, 1/2, (Exp[x] - 1)/(Exp[x] - 1 + x)]]

sol = NDSolveValue[{eq, x[0] == -1}, x, {t, 0, 6}]

Plot[sol[t], {t, 0, 6}]

Mathematica graphics


Update

If the limit needs to be calculated each time it encounters zero:

eq = With[{x = x[t]}, 
  With[{expr = (Exp[x] - 1)/(Exp[x] - 1 + x)}, 
   D[x, t] == If[x == 0, Limit[expr, x -> 0], expr]]]
$\endgroup$
  • $\begingroup$ thanks for the answer, please look at my addition to the question. It is important that the limit will be calculated each time it encounters zero. $\endgroup$ – jarhead May 2 at 14:44
  • $\begingroup$ Please also keep the answer restricted to 'NDSolve' if possible. $\endgroup$ – jarhead May 2 at 14:45
  • $\begingroup$ @jarhead Check my update. $\endgroup$ – xzczd May 2 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.