Search code examples
pythonnumpyodeodeint

Solving first-order ODE, which contains another ODE (odeint / solve_ivp in Python)


I'm trying to set up a fast numerical solver in Python for a differential problem of the form:

Equations shown clearly as an image

where r is some constant.

I want to integrate A over some time period, t of interest. However, this is complicated by the fact that the dA/dt equation includes another variable B, which itself is described by an ODE dB/dt. B is actually a vector, but I've simplified the expression to try and highlight my problems more clearly.

I currently have a solution using a manual Euler method: ie compute dB/dt (then use B = B_previous + dB/dt * dt) and manually step along using a fixed time step size dt. However, this is slow and unreliable. I imagine it would be far better to use the built-in ODE solvers in Numpy, but I'm not sure this is possible given the coupled nature of the problem I'm trying to solve?

Is this possible using Numpy odeint or solve_ivp please? And if so, can anyone suggest any pointers please! Thanks.


Solution

  • What you have is a coupled differential equation which are standard to solve using Runge kutta, Eulers, and many other methods. You can use this example to guide you in writting your python code:

    https://scipy-cookbook.readthedocs.io/items/CoupledSpringMassSystem.html

    Keep in mind that that not all equations can be solved with ODEINT. If your ODE is a "stiff" ODE then you will have to choose your algorithm precisely. The definition of a stiff ODE is not completely defined but usually they arise if you have large or non-integral powers of your dependent variable in your ODE.

    The first step in solving a coupled ODE though is to use standard methods. If they don't work then look into something else.