Ich versuche, Zerfallsgleichungen mit scipy.integrate.odeint zu lösen. Ich versuche, Anfangswerte aus einem Wörterbuch zu haben, aber es funktioniert nicht und ich bin nicht sicher, ob es funktionieren kann. Hier ist der Code Ich arbeite mit:Verwenden von scipy.integrate.odeint mit Wörterbuch [Python]
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def decay(init,t):
f0 = - init['a']/.5
f1 = init['a']/.5 - init['b']/.2
f2 = init['b']/.2
return [f0,f1,f2]
if __name__ == '__main__':
init = {'a':5, 'b':0, 'c':0}
time = np.linspace(0, 10, 101)
soln = odeint(decay, init ,time)
a = soln[:,0]
b = soln[:,1]
c = soln[:,2]
print a
print b
print c
plt.plot(time, a, color = 'g')
plt.plot(time, b, color = 'r')
plt.plot(time, c, color = 'b')
plt.show()
Es funktioniert wie erwartet, wenn anstelle eines Wörterbuchs verwende ich eine Liste wie folgt aus:
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def decay(init,t):
a,b,c = init
f0 = - a/.5
f1 = a/.5 - b/.2
f2 = b/.2
return [f0,f1,f2]
if __name__ == '__main__':
init = [5,0,0]
time = np.linspace(0, 10, 101)
soln = odeint(decay, init ,time)
a = soln[:,0]
b = soln[:,1]
c = soln[:,2]
print a
print b
print c
plt.plot(time, a, color = 'g')
plt.plot(time, b, color = 'r')
plt.plot(time, c, color = 'b')
plt.show()
Ich brauche aber ein Wörterbuch verwenden für meine Absichten. Gibt es eine Möglichkeit, ein Wörterbuch zu verwenden, um die Anfangswerte aufzurufen?
Laut der Dokumentation soll der zweite Parameter ein Array sein - 'numpy' konvertiert Liste in Array, aber es wird kein dict in ein Array konvertieren ... Also, was Sie wahrscheinlich fragen ist nicht möglich ... – mgilson
Oh, ja, ich sehe das jetzt. Gutes Auge – greenthumbtack