Search code examples
pythonscipyodeint

Dynamic generatation of equation system for odeint


I have a ODE system for odeint Python module:

def equations(p, t, lmbds):
    return np.array([-p[0] * lmbds[0] + p[1] * lmbds[1],
                     -p[1] * lmbds[1] - p[1] * lmbds[2] + p[0] * lmbds[0]])

This system corresponding to the Markov chain with two states three intensity.

The system of equations can contain a different number of equations. Is it possible to generate them on the go, depending on the number of equation entered by the user?


Solution

  • Please see approach of the creating n equations below:

    def get_equation(p, t, lmbds, n):
        """ Returns equation for provided n. """
        # Create and return equation
    
    def equations(p, t, lmbds, eq_num):
        equations = []
        for n in range(eq_num):
            equation = get_equation(p, t, lmbds, n)
            equations.append(equation)
        return np.array(*equations)