In an earlier version of the question, the problem stated only a simple system of ODE. It was then changed to a delay differential equation and the answer below is no longer valid. I leave it for future reference.
To solve a system with delay, additional python packages have to be used. For example the package JiTCDDE allows to solve this kind of equations.
A related question was asked here: Solve ODE in Python with a time-delay
Old Answer
The scipy function ode
might be what you are looking for:
Lets first define the two equation systems. One for t<t0
and one for t>t0
. We call these functions f
and f2
. Additionally, we also calculate the Jacobian matrix, which can later be used by the integrator.
def f(t,y,a,b,c,t_0):
return [b*y[0]*y[1]-c*(t-t_0), a*y[1]-b*y[0]*y[1]]
def f2(t,y,a,b,c):
return [b*y[0]*y[1], a*y[1]-b*y[0]*y[1]]
def jac_f(t,y,a,b):
return [[b*y[1],b*y[0]],[-b*y[1],a-b*y[1]]]
Then we import ode
and call the integrator two times. The first time, we integrate from our start value (I set it to t=0) until we reach t0
and then start a second integration with the equation system valid for t>t0
. We pass the last calculated values as initial conditions to the integrator and continue our integration until we reach t=4
(arbitrarily chosen).
from scipy.integrate import ode
y_res = []
t_list = []
a,b,c,t0=1,1,1,1
y0=[1,2]
t_start=0
t_fin=t0
dt=0.01
r=ode(f2,jac_f).set_integrator("vode", method="adams", with_jacobian=True)
r.set_initial_value(y0, t_start).set_f_params(a,b).set_jac_params(a,b)
while r.successful() and r.t < t_fin:
r.integrate(r.t+dt)
y_res.append(r.y)
t_list.append(r.t)
y0=y_res[-1]
t_start=t0
t_fin=4
dt=0.01
r=ode(f,jac_f).set_integrator("vode", method="adams", with_jacobian=True)
r.set_initial_value(y0, t_start).set_f_params(a,b,c,t0).set_jac_params(a,b)
while r.successful() and r.t < t_fin:
r.integrate(r.t+dt)
y_res.append(r.y)
t_list.append(r.t)
We can now plot the resulting curves:
import matplotlib.pyplot as plt
yy=np.stack(y_res)
plt.plot(t_list, yy[:,0], label="x(t)")
plt.plot(t_list, yy[:,1], label="y(t)")
plt.legend()
plt.show()
We get this nice graph:
