2014-09-19 8 views
6

Sto cercando di risolvere seguente equazione differenziale con pacchetto python PyDDE:risolvere l'equazione differenziale utilizzando Python PyDDE risolutore

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

Di seguito si riporta il codice Python per risolvere questa equazione

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 

sto ottenendo seguente error:

Errore DDE: Qualcosa non va: forse una delle variabili fornite ha il tipo sbagliato?

Errore DDE: inizializzazione del problema non riuscita!

Errore DDE: DDE non è stato inizializzato correttamente!

Nessuno

+0

il codice sia corretto, quale versione di Python stai usando? L'unica cosa che penso possa essere il problema è che o odecons, odeist o odestsc non è in realtà un array o che potrebbe essere necessario restituire solo theta nella funzione grad ode. Passare attraverso il codice e stampare i tipi di tutte le variabili. –

+0

@JaredReeves Sto usando Python 2.7.8 e ho installato PyDDE da https://github.com/hensing/PyDDE perché ho avuto un problema di installazione con la versione attuale di PyDDE. Ho provato ogni possibile cosa, tuttavia lo farò di nuovo come quello che hai menzionato. – ADK

+0

Vorrei solo usare sympy. –

risposta

1

così ho avuto uno sguardo a ciò che stava accadendo all'interno, ed entrambi gli errori

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

venire dalla seguente operazione in mancanza: carta (float, initstate) (vedi source, riga 162). Questo deriva dal fatto che Y e le altre variabili sono vettori. Per lo più questo significa che non si dovrebbe usare array([theta]) ma si dovrebbe usare theta

script completo:

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