'Error in dimension of array in interpolate scipy

I am using a package (Python language) to solve delayed differential equations (DDEs) that is available at https://zulko.wordpress.com/2013/03/01/delay-differential-equations-easy-with-python/ whose code is:

# REQUIRES PACKAGES Numpy AND Scipy INSTALLED
import numpy as np
import scipy.integrate
import scipy.interpolate
  
class ddeVar:
    """ special function-like variables for the integration of DDEs """
     
     
    def __init__(self,g,tc=0):
        """ g(t) = expression of Y(t) for t<tc """
         
        self.g = g
        self.tc= tc
        # We must fill the interpolator with 2 points minimum
        self.itpr = scipy.interpolate.interp1d(
            np.array([tc-1,tc]), # X
            np.array([self.g(tc),self.g(tc)]).T, # Y
            kind='linear', bounds_error=False,
            fill_value = self.g(tc))
             
             
    def update(self,t,Y):
        """ Add one new (ti,yi) to the interpolator """
         
        self.itpr.x = np.hstack([self.itpr.x, [t]])
        Y2 = Y if (Y.size==1) else np.array([Y]).T
        self.itpr.y = np.hstack([self.itpr.y, Y2])
        self.itpr.fill_value = Y
         
         
    def __call__(self,t=0):
        """ Y(t) will return the instance's value at time t """
         
        return (self.g(t) if (t<=self.tc) else self.itpr(t))
  
 
 
class dde(scipy.integrate.ode):
    """ Overwrites a few functions of scipy.integrate.ode"""
     
     
    def __init__(self,f,jac=None):
         
        def f2(t,y,args):
            return f(self.Y,t,*args)
        scipy.integrate.ode.__init__(self,f2,jac)
        self.set_f_params(None)
         
  
    def integrate(self, t, step=0, relax=0):
         
        scipy.integrate.ode.integrate(self,t,step,relax)
        self.Y.update(self.t,self.y)
        return self.y
         
  
    def set_initial_value(self,Y):
         
        self.Y = Y #!!! Y will be modified during integration
        scipy.integrate.ode.set_initial_value(self, Y(Y.tc), Y.tc)
  
  
  
def ddeint(func,g,tt,fargs=None):
    """ similar to scipy.integrate.odeint. Solves the DDE system
        defined by func at the times tt with 'history function' g
        and potential additional arguments for the model, fargs
    """
     
    dde_ = dde(func)
    dde_.set_initial_value(ddeVar(g,tt[0]))
    dde_.set_f_params(fargs if fargs else [])
    return np.array([g(tt[0])]+[dde_.integrate(dde_.t + dt)
                                 for dt in np.diff(tt)], dtype=object)

I have problems reproducing the third example that the author wrote on your page. The example is

from pylab import *
 
def model(Y,t,d):
    x,y = Y(t)
    xd,yd = Y(t-d)
    return array([0.5*x*(1-yd), -0.5*y*(1-xd)])
 
g = lambda t : array([1,2])
tt = linspace(2,30,20000)
fig,ax=subplots(1)
 
for d in [0, 0.2]:
    yy = ddeint(model,g,tt,fargs=(d,))
    # WE PLOT X AGAINST Y
    ax.plot(yy[:,0],yy[:,1],lw=2,label='delay = %.01f'%d)
 
ax.legend()
show()

When I run in Python3 (I am using Spyder IDLE) this example, I received the following error related to the dimensions of the array in the interpolation of scipy module (I think is that). I will not be able to solve this. Please, how make a correction in this code? Thanks

File "C:\Users\LUCIAN~1\AppData\Local\Temp/ipykernel_6024/2627939967.py", line 16, in <module>
    yy = delay.ddeint(lotka, g, tt, fargs=(d,))

  File "C:\Users\Luciano Aparecido\delay_module.py", line 81, in ddeint
    return np.array([g(tt[0])]+[dde_.integrate(dde_.t + dt)

  File "C:\Users\Luciano Aparecido\delay_module.py", line 81, in <listcomp>
    return np.array([g(tt[0])]+[dde_.integrate(dde_.t + dt)

  File "C:\Users\Luciano Aparecido\delay_module.py", line 60, in integrate
    scipy.integrate.ode.integrate(self,t,step,relax)

  File "C:\Users\Luciano Aparecido\anaconda3\lib\site-packages\scipy\integrate\_ode.py", line 433, in integrate
    self._y, self.t = mth(self.f, self.jac or (lambda: None),

  File "C:\Users\Luciano Aparecido\anaconda3\lib\site-packages\scipy\integrate\_ode.py", line 1009, in run
    y1, t, istate = self.runner(*args)

  File "C:\Users\Luciano Aparecido\delay_module.py", line 53, in f2
    return f(self.Y,t,*args)

  File "C:\Users\LUCIAN~1\AppData\Local\Temp/ipykernel_6024/2627939967.py", line 2, in lotka
    x,y = Y(t)

  File "C:\Users\Luciano Aparecido\delay_module.py", line 42, in __call__
    return (self.g(t) if (t<=self.tc) else self.itpr(t))

  File "C:\Users\Luciano Aparecido\anaconda3\lib\site-packages\scipy\interpolate\polyint.py", line 78, in __call__
    y = self._evaluate(x)

  File "C:\Users\Luciano Aparecido\anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 682, in _evaluate
    y_new = self._call(self, x_new)

  File "C:\Users\Luciano Aparecido\anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 625, in _call_linear
    y_lo = self._y[lo]

IndexError: index 2 is out of bounds for axis 0 with size 2


capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.
capi_return is NULL
Call-back cb_f_in_dvode__user__routines failed.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source