あなたのコードにはいくつかの問題がありますが、ドキュメントストリングをodeint
もっと注意深く読めば、そのほとんどは自分で解決できるかもしれません。
始めるために、以下はスカラー微分方程式を で解く簡単な例ですodeint
。関数を理解しようとする (場合によってはデバッグしようとする) 代わりに、非常に単純な方程式を使用します。方程式 dy/dt = a * y を初期条件 y(0) = 100 で解きます。この例が機能したら、修正fun
して問題を解決できます。
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def fun(y, t, a):
"""Define the right-hand side of equation dy/dt = a*y"""
f = a * y
return f
# Initial condition
y0 = 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()
プロットは次のとおりです。
の使用のより複雑な例は、 SciPy クックブック(「常微分方程式」というラベルの付いた箇条書きまでスクロールダウン)odeint
で見つけることができます。