0
我试图解决使用scipy.integrate.odeint衰变方程。我试图从字典中获得初始值,但它不起作用,我不确定它是否可以工作。这里是我正在使用的代码:使用scipy.integrate.odeint与字典[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()
它的工作原理是,如果预期的,而不是一本字典我用像这样的列表:
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()
不过,我需要使用一个字典我的目的。有没有办法使用字典来调用初始值?
根据文档,第二个参数应该是一个数组 - 'numpy'将列表转换为数组非常好,但它不会将字典转换为数组...所以您要求的可能是是不可能的... – mgilson
哦,是的,现在我明白了。好眼睛 – greenthumbtack