Search code examples
pythonnumpyscipydifferential-equationsnumba

Solve differential equation in Python when I don't know the derivative analytically


I'm trying to solve a first-order ODE in Python:

http://www.sciweavers.org/upload/Tex2Img_1510767778/render.png

where Gamma and u are square matrices. I don't explicitly know u(t) at all times, but I do know it at discrete timesteps from doing an earlier calculation.

Every example I found of Python's solvers online (e.g. this one for scipy.integrate.odeint and scipy.integrate.ode) know the expression for the derivative analytically as a function of time.

Is there a way to call these (or other differential equation solvers) without knowing an analytic expression for the derivative?

For now, I've written my own Runge-Kutta solver and jitted it with numba.


Solution

  • You can use any of the SciPy interpolation methods, such as interp1d, to create a callable function based on your discrete data, and pass it to odeint. Cubic spline interpolation,

    f = interp1d(x, y, kind='cubic')
    

    should be good enough.