擅长:python、mysql、java
<p>这是在scipy1.0中发布的<a href="http://scipy.github.io/devdocs/generated/scipy.integrate.solve_ivp.html" rel="nofollow noreferrer">the new ODE solvers</a>的方法:</p>
<pre><code>from functools import partial
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
def fun_fixed(t, x, param):
return x - param
sol_fixed = solve_ivp(
partial(fun_fixed, param=0), (0, 3), [10.0], dense_output=True)
def fun_param(t, x, fun):
return -x + fun(t)
def f_param(t):
return t
sol_param = solve_ivp(
partial(fun_param, fun=f_param), (0, 3), [10.0], dense_output=True)
t = np.linspace(0, 3, num=16)
plt.figure(figsize=(8, 5))
plt.plot(t, sol_fixed.sol(t)[0], 'o-', alpha=0.7, label='ode1')
plt.plot(t, sol_param.sol(t)[0], 's-.', label='ode3')
plt.legend()
</code></pre>
<p><a href="https://i.stack.imgur.com/PPa8p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PPa8p.png" alt="Two solutions"/></a></p>