您的代码有几个问题,如果更仔细地阅读文档字符串
odeint,您可能可以解决大多数问题。
为了帮助您入门,以下是使用求解标量微分方程的简单示例
odeint。我将尝试使用一个非常简单的方程式,而不是尝试去理解(并可能调试)您的函数。我将用初始条件y(0)=
100求解方程dy / dt = a * y。一旦使本示例生效,就可以进行修改
fun以解决问题。
import numpy as npfrom scipy.integrate import odeintimport matplotlib.pyplot as pltdef fun(y, t, a): """Define the right-hand side of equation dy/dt = a*y""" f = a * y return f# Initial conditiony0 = 100.0# Times at which the solution is to be computed.t = np.linspace(0, 1, 51)# Parameter value to use in `fun`.a = -2.5# Solve the equation.y = odeint(fun, y0, t, args=(a,))# Plot the solution. `odeint` is generally used to solve a system# of equations, so it returns an array with shape (len(t), len(y0)).# In this case, len(y0) is 1, so y[:,0] gives us the solution.plt.plot(t, y[:,0])plt.xlabel('t')plt.ylabel('y')plt.show()这是情节:
odeint在SciPy
Cookbook中可以找到使用的更复杂的示例(向下滚动到标有“常微分方程”的项目符号)。



