<p><a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.ode.html" rel="nofollow noreferrer">integrate.ode</a>的接口不像更简单的方法<a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html#scipy.integrate.odeint" rel="nofollow noreferrer">odeint</a>那样直观,但是它不支持选择ODE积分器。主要的区别是<code>ode</code>不为您运行循环;如果您需要在一堆点上找到一个解决方案,则必须说明在哪些点上,并一次计算一个点。</p>
<pre><code>import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
def vdp1(t, y):
return np.array([y[1], (1 - y[0]**2)*y[1] - y[0]])
t0, t1 = 0, 20 # start and end
t = np.linspace(t0, t1, 100) # the points of evaluation of solution
y0 = [2, 0] # initial value
y = np.zeros((len(t), len(y0))) # array for solution
y[0, :] = y0
r = integrate.ode(vdp1).set_integrator("dopri5") # choice of method
r.set_initial_value(y0, t0) # initial values
for i in range(1, t.size):
y[i, :] = r.integrate(t[i]) # get one more value, add it to the array
if not r.successful():
raise RuntimeError("Could not integrate")
plt.plot(t, y)
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/3woiJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3woiJ.png" alt="solution"/></a></p>