1.5.12.4. 简单常微分方程的积分

求解常微分方程 dy/dt = -2y,其中 t 范围为 0..4,初始条件为 y(t=0) = 1。

Solution of Initial Value Problem
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
def f(t, y):
return -2 * y
t_span = (0, 4) # time interval
t_eval = np.linspace(*t_span) # times at which to evaluate `y`
y0 = [
1,
] # initial state
res = sp.integrate.solve_ivp(f, t_span=t_span, y0=y0, t_eval=t_eval)
plt.figure(figsize=(4, 3))
plt.plot(res.t, res.y[0])
plt.xlabel("t")
plt.ylabel("y")
plt.title("Solution of Initial Value Problem")
plt.tight_layout()
plt.show()

脚本总运行时间:(0 分钟 0.078 秒)

由Sphinx-Gallery生成的图库