gpt4 book ai didi

Python odeint,参数

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:31 29 4
gpt4 key购买 nike

我正在尝试求解一个简单的方程: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

关于Python odeint,参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110881/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com