简体   繁体   English

使用Python PyDDE求解器求解微分方程

[英]Solve Differential equation using Python PyDDE solver

I am trying to solve following differential equation using python package PyDDE: 我试图使用python包PyDDE解决以下微分方程:

dy[i]/dt = w[i] + K/N * \sum{j=1toN} sin(y[j] -y[i]), where i = 1,2,3,4...N=50

Below is the python code to solve this equation 下面是解决这个等式的python代码

from numpy import random, sin, arange, pi, array, zeros
import PyDDE.pydde as p

def odegrad(s, c, t):
    global N
    K = c[0]
    theta = s[0]
    w = random.standard_cauchy(N)
    for i in range(N):
        coup_sum = 0.0
        for j in range(N):
            coup_sum += sin(theta[j] - theta[i])
        theta[i] = w[i] + (K*coup_sum)/(float (N))
    return array([theta])

# constant parameters
global N
N = 50
K = 1.0
# initial values for state theta
theta0 = zeros(N, float)
for i in range(N):
    theta0[i] = random.uniform(0, 2*pi)

odecons = array([K])
odeist = array([theta0])
odestsc = array([0.0])

ode_eg = p.dde()
ode_eg.dde(y=odeist, times=arange(0.0, 300.0, 1.0), 
       func=odegrad, parms=odecons, 
       tol=0.000005, dt=1.0, hbsize=0, nlag=0, ssc=odestsc)
ode_eg.solve()
print ode_eg.data

I am getting following error: 我收到以下错误:

DDE Error: Something is wrong: perhaps one of the supplied variables has the wrong type? DDE错误:有些问题:提供的变量之一可能是错误的类型?

DDE Error: Problem initialisation failed! DDE错误:问题初始化失败!

DDE Error: The DDE has not been properly initialised! DDE错误:DDE未正确初始化!

None 没有

So I have had a look at what was going on internally, and both errors 所以我看看内部发生了什么,以及两个错误

DDE Error: Something is wrong: perhaps one of the supplied variables has the wrong type?
DDE Error: Problem initialisation failed!

come from the following operation failing: map(float,initstate) (see the source , line 162). 来自以下操作失败:map(float,initstate)(参见源代码 ,第162行)。 This comes from the fact that Y and your other variables are vectors. 这来自Y和你的其他变量是向量的事实。 Mostly this means that you should not use array([theta]) but you should use theta 大多数情况下,这意味着你不应该使用array([theta])但你应该使用theta

Full script: 完整脚本:

from numpy import random, sin, arange, pi, array, zeros
import PyDDE.pydde as p

def odegrad(s, c, t):
    global N
    K = c[0]
    #Change here
    theta = s
    w = random.standard_cauchy(N)
    for i in range(N):
        coup_sum = 0.0
        for j in range(N):
            coup_sum += sin(theta[j] - theta[i])
        theta[i] = w[i] + (K*coup_sum)/(float (N))
    #Change here
    return theta

# constant parameters
global N
N = 50
K = 1.0
# initial values for state theta
theta0 = zeros(N, float)
for i in range(N):
    theta0[i] = random.uniform(0, 2*pi)

odecons = array([K])
#Change here
odeist = theta0
odestsc = array([0.0])

ode_eg = p.dde()
ode_eg.dde(y=odeist, times=arange(0.0, 300.0, 1.0), 
       func=odegrad, parms=odecons, 
       tol=0.000005, dt=1.0, hbsize=0, nlag=0, ssc=odestsc)

#You should not use this line, as the last step in ode_eg.dde() is solve.
#ode_eg.solve()
print ode_eg.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM