Python odeint,参数

时间:2016-01-31 06:02:31

标签: python arrays parameters odeint

我试图在python中解决一个简单的等式:dM/dr = r*p(r)

p的某些值r的值为p(0)=1

p(1)=3p(2)=5p(3)=7p(4)=9p(5)=11p

我尝试使用以下代码,但是我收到了错误

  

func(6)返回的数组大小与大小不匹配   y0(1)。

我认为问题在于我没有正确地将r值与from scipy import integrate import numpy as np r = np.array([0, 1, 2, 3, 4, 5]) p = np.array([1, 3, 5, 7, 9, 11]) def deriv (z, r, data): M = r*p return M init = np.array([0]) soln = integrate.odeint(deriv, init, p, (r,), full_output=True) print soln 值匹配。应该只有一个初始条件,因为我只想解决一个方程式。任何帮助将不胜感激。

这是我的代码:

{{1}}

1 个答案:

答案 0 :(得分:2)

您看到此错误,因为init的大小deriv()返回的数组大小匹配。

要解决此问题,请更改以下行

init = np.array([0])

init = np.array([0, 0, 0, 0, 0, 0])  

有关使用' odeint'的更多示例,请参阅:
http://scipy-cookbook.readthedocs.org/items/numpy_scipy_ordinary_differential_equations.html
http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html