3

I'm trying to solve non-linear ODE in the form of dy/dt = a(y, t) * y(t) with scipy.integrate.ode. I'm getting good results solving this kind of equation, but I wish to optimize my code.

Currently, my function is defined as f(y, t) = a(y, t)*y(t), and returns dy_dt. The problem is that I wish to get a(y,t) as well.

One solution would be to solve the problem, getting y(t) and re-computing a(y, t) for each desired time step. But this isn't highly efficient since a(y, t) has already been computed to solve the problem.

Is there a way I can get both dy_dt and a(y,t) in an efficient way simultaneously?

I've already read the DOC of scipy.integrate.ode, but I can't find any solution.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
tiwock
  • 31
  • 1
  • 2
    There is no guarantee that `a(y,t)` is actually evaluated at the points that `odeint` returns. The internal grid can be in any relation to the external grid for which the output is produced. – Lutz Lehmann May 24 '22 at 17:05
  • I would recommend to have a separate function to compute `a(t, y)` and profile your code. If the profiling shows that calling `a(y, t)` is not too expensive compared to solving `dy/dt`, just call it again after solving the ODE system. A way to optimize that final call is to design `a(t, y)` in such a way that it can operate on `t` and `y` either as scalars (during the solution of `dy/dt`) or as arrays (last call). In the latter case, it is ideal that the function uses vectorized operations, which will speed up the computation of your coefficient – Daniel Casas-Orozco Jun 23 '22 at 01:54

0 Answers0