[英]Solve Differential equation using Python PyDDE solver
我試圖使用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
下面是解決這個等式的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
我收到以下錯誤:
DDE錯誤:有些問題:提供的變量之一可能是錯誤的類型?
DDE錯誤:問題初始化失敗!
DDE錯誤:DDE未正確初始化!
沒有
所以我看看內部發生了什么,以及兩個錯誤
DDE Error: Something is wrong: perhaps one of the supplied variables has the wrong type?
DDE Error: Problem initialisation failed!
來自以下操作失敗:map(float,initstate)(參見源代碼 ,第162行)。 這來自Y和你的其他變量是向量的事實。 大多數情況下,這意味着你不應該使用array([theta])
但你應該使用theta
完整腳本:
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.