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:
        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?

  • 1
    $\begingroup$ 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? $\endgroup$ Nov 9 '21 at 7:39
  • 1
    $\begingroup$ 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 $\endgroup$ Nov 9 '21 at 7:47
  • $\begingroup$ 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. $\endgroup$
    – Hideshi
    Nov 9 '21 at 8:02
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 9 '21 at 8:16
  • 1
    $\begingroup$ Stackoverflow would be a good place for this question, ignoring any of the science-related stuff. $\endgroup$ Nov 9 '21 at 14:17

Your Answer

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

Browse other questions tagged or ask your own question.