为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 python 中找到 f(x,y) 沿 x 和 y 的偏导数

如何在 python 中找到 f(x,y) 沿 x 和 y 的偏导数

三国纷争 2021-12-17 17:05:23
我在 (xx,yy) 网格中定义了一个二维函数 f(x,y)。我想在数值上获得它的偏导数,如下所示。请注意, np.gradient 没有完成这项工作,因为它沿每个轴返回一个向量场。我怎样才能做到这一点?这是我的代码:import numpy as npimport matplotlib.pyplot as pltx = 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 jobdf=np.array(df)print(df.shape)# h = plt.contourf(x,y,df)   #This is what I want to plot.# plt.show()
查看完整描述

1 回答

?
杨魅力

TA贡献1811条经验 获得超6个赞

您需要调用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()

输出:

//img1.sycdn.imooc.com//61bc52f900015c8705600405.jpg

查看完整回答
反对 回复 2021-12-17
  • 1 回答
  • 0 关注
  • 622 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号