一、 问题描述
在Python中绘制常微分方程的Direction Field
二、示例
常微分方程如下
y
′
=
3
x
2
e
−
y
y' = 3x^{2}e^{-y}
y′=3x2e−y
代码及生成的图片如下
def derivative(x, y):
return 3*(x**2)*(math.e**(-y))
def draw(x_start, x_end, y_start, y_end):
X, Y = np.meshgrid(np.linspace(x_start, x_end, 20), np.linspace(y_start, y_end, 20))
U = 1.0
V = derivative(X, Y)
# Normalize arrows
N = np.sqrt(U ** 2 + V ** 2)
U = U / N
V = V / N
plt.quiver(X, Y, U, V, color='r')
plt.title('Direction Field')
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
draw(-0.9999, 100, -3, 13)
三、方法说明
此方法适用于以上形式微分方程,需要调整的主要参数为一阶导函数以及 x , y x,y x,y范围
重点部分为函数np.meshgrid()
以及plt.quiver()