Search code examples
pythonscipyodeodeint

using scipy odeint on equations with a phase shifted variable


Basically... I need a way to include a phase shift in my differential equations. That is, I have in the definition of my system function which returns dY/dt something like Y(t-3). Like this differential equation:

dY/dt = a*Y(t) + b*Y(t-tau)

Now if I try to write this as the system definition function for passing to scipy.odeint, I am lost:

def eqtnSystem(A,t):
    Y   = A
    a   = 1
    b   = 5
    tau = 3
    return a*Y + b*???       # how do I Y(t-tau) ?

That's basically it. I really hope there is an easy answer, but I couldn't seem to track one down.

Specifically... I am attempting to numerically calculate the solution for the system defined by the following function:

def etaFunc(A,t): 
    #...definition of all those constants is here...
    return array([(gamma[0,0]*xi(t-theta[0])[0] - eta[0] + zeta[0])/tau[0],\
           (gamma[1,1]*xi(t-theta[1])[1] - eta[1] + zeta[1])/tau[1],\
           (gamma[2,2]*xi(t-theta[2])[2] - eta[2] + zeta[2])/tau[2],\
           (   beta[3,0]*pastEta(t-theta[3])[0] \
             + beta[3,1]*pastEta(t-theta[4])[1] \
             + beta[3,2]*pastEta(t-theta[5])[2] -eta[3]+ zeta[3])/tau[3],\
           (   beta[4,3]*pastEta(t-theta[6])[3] \
             + beta[4,2]*pastEta(t-theta[7])[2] - eta[4] + zeta[4])/tau[4]])

This function is then later given to odeint like this:

ETA = integrate.odeint(etaFunc,initCond,time)

and then I can get out each individual component of ETA (like eta_0) like this: ETA[:,0].

The problem I am having here, is with pastEta(t-theta[?]). For right now, this is a function which attempts to find already calculated values of eta (for when start_time < t-theta[?] < t and theta[?] > 0. This isn't working very well.

I see in this case I could find each component of eta individually and then get calculated values for previously calculated eta components (eta_0,eta_1,eta_2) to calculate eta_3 and similarly for eta_4, but this is not ideal since it takes away the ability for me to 'plug-and-play' any general formulas.


Solution

  • There are a number of existing libraries and examples for doing this.

    http://www.google.fi/search?q=python+delay+differential+equation gives me: