我有一个等式dy/dx = x + y/5
和一个初始值,y(0) = -3
.
我想知道如何使用 pyplot 绘制该函数的精确图形。
我也有一个x = np.linspace(0, interval, steps+1)
我想用它作为 x 轴。所以我只寻找 y 轴值。
提前致谢。
为了完整起见,这种方程可以很容易地进行数值积分,使用scipy.integrate.odeint
.
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
# function dy/dx = x + y/5.
func = lambda y,x : x + y/5.
# Initial condition
y0 = -3 # at x=0
# values at which to compute the solution (needs to start at x=0)
x = np.linspace(0, 4, 101)
# solution
y = odeint(func, y0, x)
# plot the solution, note that y is a column vector
plt.plot(x, y[:,0])
plt.xlabel('x')
plt.ylabel('y')
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)