【问题标题】:How to find Partial derivative of f(x,y) along x and y: del^2 f(x,y)/[del(x)][del (y)] in python如何在 python 中找到 f(x,y) 沿 x 和 y 的偏导数:del^2 f(x,y)/[del(x)][del (y)]
【发布时间】:2019-03-27 11:28:02
【问题描述】:

我在 (xx,yy) 网格中定义了一个二维函数 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()

【问题讨论】:

    标签: python calculus


    【解决方案1】:

    您需要拨打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()
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2022-06-19
      相关资源
      最近更新 更多