我正在使用以下Python代码(取自我记不起的另一篇SO帖子)来绘制微分方程dy / dx = 1 /(x + y)的斜率字段:
import numpy as np
from matplotlib import pyplot as plt
# Differential equation
# diff = y'= y/x (or say x+y)
def diff(x,y):
if (x+y == 0):
return 0
else:
return 1/(x+y) # try also x+y
x = np.linspace(-3,3,25)
y = np.linspace(-3,3,25)
# use x,y
for j in x:
for k in y:
slope = diff(j,k)
domain = np.linspace(j-0.07,j+0.07,2)
def fun(x1,y1):
z = slope*(domain-x1)+y1
return z
plt.plot(domain,fun(j,k),solid_capstyle='projecting',solid_joinstyle='bevel')
plt.title("Slope field y'")
plt.grid(True)
plt.show()
print("End of the program")
我现在想通过这个相同的斜率场绘制一条曲线(例如,y = -x - 1)。
如果人们可以请花时间来证明我会如何解决这个问题,我将不胜感激。