# Adding a limit in NDSolve to avoid division by zero

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?

Look for simplicity at the following case

NDSolve[{x'[t] == (Exp[x[t]] - 1)/(Exp[x[t]] - 1 + x[t]),
x == 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.

• 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. – Chris K May 2 at 15:31
• @ChrisK, elsewhere, actually all along the solution periodically. I gave the initial condition as an example. – jarhead May 2 at 15:49
• Could you give an example where it doesn't work x != 0? – Chris K May 2 at 16:14

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 == -1}, x, {t, 0, 6}]

Plot[sol[t], {t, 0, 6}] # 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]]]

• 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. – jarhead May 2 at 14:44
• Please also keep the answer restricted to 'NDSolve' if possible. – jarhead May 2 at 14:45
• @jarhead Check my update. – xzczd May 2 at 14:57