Backward integration in time using scipy odeint

前端 未结 3 821
攒了一身酷
攒了一身酷 2021-01-15 11:01

Is it possible to integrate any Ordinary Differential Equation backward in time using scipy.integrate.odeint ? If it is possible, could someone tell me what should be the ar

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-15 11:50

    odeint handles negative values of the t argument. No special treatment is needed.

    Here's an example:

    import numpy as np
    from scipy.integrate import odeint
    import matplotlib.pyplot as plt
    
    
    def mysys(z, t):
        """A slightly damped oscillator."""
        return [z[1] - 0.02*z[0], -z[0]]
    
    
    if __name__ == "__main__":
        # Note that t starts at 0 and goes "backwards"
        t = np.linspace(0, -50, 501)
    
        z0 = [1, 1]
        sol = odeint(mysys, z0, t)
    
        plt.plot(t, sol)
        plt.xlabel('t')
        plt.show()
    

    The plot: Solve an ODE backward in time

提交回复
热议问题