Menu

Define a variable dependant finction in differential equations

Help
2016-11-04
2016-11-18
  • A Ziaeemehr

    A Ziaeemehr - 2016-11-04

    I am completely new in 'PyDSTool' so excuse me if it seems silly. I want to define two nonlinear differential equations . There is a nonlinear function that takes a variable as input:

     S(x1) = 1/(1+exp(x1-a))
    

    So I wrote:

    from PyDSTool import *
    icdict = {'x':0,'y':0.2}
    pardict = {
      'c1': 16.0,
      'c2': 12,
      'c3':15,
      'c4':3,
      'ax':1.3,
      'ay':2.,
      'rx':1.,
      'ry':1.,
      'P':1.25,
      }
    
    def Func(x,a): #nonlinear function
      return (['x'],'1./(1. + exp(x-%.2f))'% a)
    
    auxfndic = {'funx': Func(c1*x -c2*y+P,ax),'funy': Func(c3*x -c4*y,ay)}
    xstr = ' (-x + (1 - rx * x)* funx(x,y))'
    ystr = ' (-y + (1 - ry * y)* funy(x,y))'
    vardict = {'x': xstr,'y':ystr}
    
    DSargs = args()
    DSargs.name = 'test_Equations'
    DSargs.ics = icdict
    DSargs.pars = pardict
    DSargs.tdata = [0, 200]
    DSargs.varspecs = vardict
    
    DS = Generator.Vode_ODEsystem(DSargs)
    
    traj = DS.compute('test')
    pts = traj.sample()
    
    plt.plot(pts['t'], pts['x'], 'k', label='x')
    plt.legend()
    plt.show()
    

    c1 can not be read by the function.

     NameError: name 'c1' is not defined
    

    I don't know how to define Func inside the ODE that takes a variable as input. Thanks for any guide.
    Link

     
  • Rob Clewley

    Rob Clewley - 2016-11-10

    This is a python error rather than a PyDSTool error. You have to keep track of what names belong in what namespace. For PyDSTool, you additionally have the "virtual" namespace of variables, parameters, etc., which are not regular python names. However, your use of c1 in this script is as if it was a python name, and it is nowhere defined as such. There are a couple of different ways to declare functions, as described in the docs (symbolic objects and strings) but it will be easier to not use python functions, i.e. your Func. Instead, just substitute the return value from your function into your auxfndic value as one auxiliary function. Then, when you use the function, you can pass the expression involving your params as an argument, all as part of a string. Look again at the example codes provided and spot the difference in syntax. Hope that helps!

     
  • A Ziaeemehr

    A Ziaeemehr - 2016-11-18

    Thanks Dr. Clewley
    here is my edit:

    from PyDSTool import *
    icdict = {'xp':0,'yp':0.2}
    pardict = {
      'c1': 16.0,
      'c2': 12,
      'c3':15,
      'c4':3,
      'ax':1.3,
      'ay':2.,
      'rx':1.,
      'ry':1.,
      'P':1.25,
      }
    
    funx =  '1./(1+ exp((c1*xp -c2*yp + P)-ax))'
    funy =  '1./(1+ exp((c3*xp -c4*yp)-ay))'
    
    xstr = '-xp + (1 - rx * xp)*' + funx
    ystr = '-yp + (1 - ry * yp)*' + funy
    vardict = {'xp': xstr,'yp':ystr}
    
    DSargs = args()
    DSargs.name = 'test_Equations'
    DSargs.ics = icdict
    DSargs.pars = pardict
    DSargs.tdata = [0, 200]
    DSargs.varspecs = vardict
    
    DS = Generator.Vode_ODEsystem(DSargs)
    
    traj = DS.compute('test')
    pts = traj.sample()
    
    plt.plot(pts['t'], pts['xp'], 'k', label='x')
    
     

Log in to post a comment.