using ODEINT to solve two coupled systems of N differential equations ( each ) , how to fit initial conditions?

霸气de小男生 提交于 2019-12-11 23:37:28

问题


I am trying to solve numerically a system composed of two systems which is coupled.
The general instructions are detailed in this picture.

So far I have written the following code:

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
%matplotlib inline

ep=0.1
a=0.2
N = 200
T=0.001
i = np.arange(N)
theta = (2*np.pi)*i/N

I0 = 0.2
J0 = 0
J1 = 2.5
delta_theta = np.subtract.outer(theta, theta)
J = (J0 + J1*np.cos(delta_theta)+J1*ep*np.sin(delta_theta))/(2*N)
t = np.linspace(0, 2.5, 1000)

np.random.seed(123)
rL0 = np.random.rand(N)
rR0 = np.random.rand(N)

V=[1,2,3]
for tr in V:
    def v_func(t1):
        if t1>0.499 and t1<1.5:
            if tr==1:
                v=1
            elif tr==2:
                v=2*(t1-0.5)
            else:
                v=np.cos(2*np.pi*t1)
        else:
            v=0
        return v

    def vectors2(rL,rR, t, IL,IR, J):
        s1 = J @ rL
        s2 = J @ rR
        drLdt = (-rL + np.maximum(I0*(1-a*v_func(t)) + s1 + s2, 0))/T
        drRdt = (-rR + np.maximum(I0*(1+a*v_func(t)) + s1 + s2, 0))/T
        return drLdt,drRdt

    rL,rR = odeint(vectors2, [rL0,rR0], t,args=(a,T))

    plt.figure(figsize=[15,8])

    for i in [0, 100, 200, 300, 399]:
        plt.plot(t, rL[:, i], label='r(%d)' % i)

plt.xlabel('t')
plt.ylabel('rate of firing')
plt.legend(shadow=True)
plt.grid()
plt.title('general observation of the dynamics : development through time of arbitrary neurons \n random initial conditions, I1=0.001')
plt.show()

And I am receiving this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-236-7ff71f3388ef> in <module>
     43         return drLdt,drRdt
     44 
---> 45     rL,rR = odeint(vectors2, [rL0,rR0], t,args=(a,T))
     46 
     47     plt.figure(figsize=[15,8])

~/anaconda3/lib/python3.7/site-packages/scipy/integrate/odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
    231                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
    232                              ixpr, mxstep, mxhnil, mxordn, mxords,
--> 233                              int(bool(tfirst)))
    234     if output[-1] < 0:
    235         warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."

ValueError: Initial condition y0 must be one-dimensional.

Basically what I did was creating two matrices rR and rL, each of them are a system of N ODE's, and I tried to pass them into ODEINT. I believe the error occurred since I didn't know how to pass in the initial conditions (two lists composed of N elements each rL0, rR0).

Maybe my method is all wrong from the beginning. Any help would be appreciated.

来源:https://stackoverflow.com/questions/56039218/using-odeint-to-solve-two-coupled-systems-of-n-differential-equations-each

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!