我正在尝试求解一个简单的方程:dM/dr = r*p(r) 在 python 中。
我在 r 的某些值处有 p 的值:
p(0)=1, p(1)=3, p(2)=5, p( 3)=7, p(4)=9, p(5)=11。
我尝试使用以下代码,但出现错误
The size of the array returned by func (6) does not match the size of y0 (1).
我认为问题在于我没有将 p 值与 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
请您参考如下方法:
您看到此错误是因为 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
