I'm trying to solve decay equations using scipy.integrate.odeint. I'm trying to have initial values from a dictionary, but it isn't working and I'm not sure if it can work. Here is the code I'm working with:
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()
It works as expected if instead of a dictionary I use a list like this:
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()
However, I need to be using a dictionary for my purposes. Is there a way to use a dictionary to call the initial values?
numpyconverts list to array just fine, but it won't convert a dict to an array ... So what you're asking for probably isn't possible...