Solve a Differential Equation Using Python's PyDDE Solution

I am trying to solve the following differential equation using the python PyDDE package:

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 python code to solve this equation

 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 get the following error:

DDE error: something is wrong: is it possible that one of the supplied variables is of the wrong type?

DDE Error: Initialization of the Problem Failed!

DDE Error: DDE was not initialized correctly!

None

+6
source share
1 answer

So, I looked at what is happening inside, and both errors

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

comes from the following operation: map (float, initstate) (see source , line 162). This is because Y and your other variables are vectors. This basically means that you should not use array([theta]) , but you should use 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 
+1
source

Source: https://habr.com/ru/post/975513/


All Articles