我在(xx,yy)网格中定义了一个2D函数f(x,y)。我想从数字上得到它的偏导数,如下所示。注意,np.gradient不执行这项工作,因为它沿着每个轴返回一个向量字段。

我怎么能这么做?这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-5, 5, 0.1)
y = np.arange(-4, 4, 0.1)
xx, yy = np.meshgrid(x, y, sparse=True)
f = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,f)
plt.show()
df=np.gradient(f,y,x) #Doesn't do my job
df=np.array(df)
print(df.shape)
# h = plt.contourf(x,y,df) #This is what I want to plot.
# plt.show()发布于 2019-03-27 11:36:37
您需要调用np.gradient两次:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-5, 5, 0.1)
y = np.arange(-4, 4, 0.1)
xx, yy = np.meshgrid(x, y, sparse=True)
f = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,f)
plt.show()
dfy = np.gradient(f, y, axis=0)
dfxy = np.gradient(dfy, x, axis=1)
print(dfxy.shape)
# (80, 100)
h = plt.contourf(x, y, dfxy)
plt.show()输出:

https://stackoverflow.com/questions/55376102
复制相似问题