# Anyway to escape ODEintWarning (scipy)?

I am trying to fit a differential equation to some data and obtain the parameters of the underlying model. This requires me to try out various parameter values, but this often gives me an ODEintWarning. Having a few of these warnings is OK, but having too many will cause my program to crash quite often (I work using Spyder and if I click somewhere on the GUI while the program is running, Spyder will freeze).

Is there a way to detect an ODEintWarning and escape it? In pseudocode, I essentially want something like:

for parameter in parameters_list:
try:
x = odeint(dxdt, x0,t, *parameters)
except (at the first sign of an) ODEintWarning:
pass (and go on to the next set of parameters to be tested)


Or, is my best option to detect bad parameter values in advance and try not to feed them into ODEint in the first place?

• Have you tried if switching to solve_ivp with method LSODA gives similar enough results with more useful warning/error behavior? // Did you read the documentation to find and apply the mxhnil, if it does what the doc line appears to say? Nov 9 '21 at 7:39
• Usually you get warning messages when the solution diverges. You could try to avoid that by modifying the ODE function to be sub-linear outside the region of interest where the adapted solution is expected to lie. // In a related but different domain, BVP solvers ameliorate this kind of problem by following a multiple-shooting approach Nov 9 '21 at 7:47
• Thanks for the comment, I haven't tried solve_ivp and will try it out. (I might also try RK45 as I want a faster screening method) I haven't changed mxhnil, because I don't mind odeint printing messages. As far as I understand, the problem is that odeint can't find good timesteps, and I want it to give up if it can't, instead of trying endlessly. Nov 9 '21 at 8:02
• That's why keeping it sub-linear is important. In the most simple case, divide the derivatives vector, after it is constructed, by a suitable power of max(1,norm(x)/R), where R is chosen so that the solution is inside norm(x)<R with a wide margin. Nov 9 '21 at 8:16
• Stackoverflow would be a good place for this question, ignoring any of the science-related stuff. Nov 9 '21 at 14:17