偏导数的可视化:以f(x, y) = sin(x) * cos(y)为例
创作时间:
作者:
@小白创作中心
偏导数的可视化:以f(x, y) = sin(x) * cos(y)为例
引用
CSDN
1.
https://blog.csdn.net/flyfish1986/article/details/141138317
偏导数是多元函数中一个重要的概念,它描述了函数在某一点沿某个方向的变化率。本文将通过Python代码,使用matplotlib库,对函数f(x, y) = sin(x) * cos(y)的偏导数进行可视化展示。
函数f(x, y) = sin(x) * cos(y)的可视化
import numpy as np
from sympy import lambdify, sin, cos
from sympy.abc import x, y
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
class FunctionVisualization:
def __init__(self, num=301):
self.num = num
self.x_array = np.linspace(-3, 3, self.num)
self.y_array = np.linspace(-3, 3, self.num)
self.xx, self.yy = np.meshgrid(self.x_array, self.y_array)
self.f_xy = sin(x) * cos(y)
self.f_xy_fcn = lambdify([x, y], self.f_xy)
def compute_function_values(self):
return self.f_xy_fcn(self.xx, self.yy)
def plot_function(self, f_zz):
fig = plt.figure(figsize=(12, 6))
contour_color = '#008080'
contourf_color = 'viridis'
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(self.xx, self.yy, f_zz, color=[0.7, 0.7, 0.7], linewidth=0.25)
colorbar = ax.contour(self.xx, self.yy, f_zz, 20, cmap=contourf_color)
ax.contour(self.xx, self.yy, f_zz, levels=[0], colors=contour_color, linestyles='-')
fig.colorbar(colorbar, ax=ax)
ax.set_proj_type('ortho')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel(r'$f(x, y)$')
ax.view_init(azim=-135, elev=30)
ax.grid(False)
ax2 = fig.add_subplot(1, 2, 2)
colorbar = ax2.contourf(self.xx, self.yy, f_zz, 20, cmap=contourf_color)
ax2.contour(self.xx, self.yy, f_zz, levels=[0], colors=contour_color, linestyles='-')
fig.colorbar(colorbar, ax=ax2)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_aspect('equal', adjustable='box')
plt.tight_layout()
plt.show()
def visualize_function(self):
f_zz = self.compute_function_values()
self.plot_function(f_zz)
visualization = FunctionVisualization()
visualization.visualize_function()
偏导数的可视化
import numpy as np
from sympy import lambdify, diff, sin, cos
from sympy.abc import x, y
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
class FunctionVisualization:
def __init__(self, num=301):
self.num = num
self.x_array = np.linspace(-3, 3, self.num)
self.y_array = np.linspace(-3, 3, self.num)
self.xx, self.yy = np.meshgrid(self.x_array, self.y_array)
self.f_xy = sin(x) * cos(y)
self.f_xy_fcn = lambdify([x, y], self.f_xy)
def compute_function_values(self):
return self.f_xy_fcn(self.xx, self.yy)
def compute_partial_derivative(self, variable):
df_d_var = diff(self.f_xy, variable)
df_d_var_fcn = lambdify([x, y], df_d_var)
return df_d_var_fcn(self.xx, self.yy)
def plot_partial_derivative(self, variable, df_d_var_zz):
fig = plt.figure(figsize=(12, 6))
contour_color = '#FFA500'
contourf_color = 'coolwarm'
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(self.xx, self.yy, df_d_var_zz, color=[0.7, 0.7, 0.7], linewidth=0.25)
colorbar = ax.contour(self.xx, self.yy, df_d_var_zz, 20, cmap=contourf_color)
ax.contour(self.xx, self.yy, df_d_var_zz, levels=[0], colors=contour_color, linestyles='-')
fig.colorbar(colorbar, ax=ax)
ax.set_proj_type('ortho')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel(fr'$\frac{{\partial f}}{{\partial {variable}}}$')
ax.view_init(azim=-135, elev=30)
ax.grid(False)
ax2 = fig.add_subplot(1, 2, 2)
colorbar = ax2.contourf(self.xx, self.yy, df_d_var_zz, 20, cmap=contourf_color)
ax2.contour(self.xx, self.yy, df_d_var_zz, levels=[0], colors=contour_color, linestyles='-')
fig.colorbar(colorbar, ax=ax2)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_aspect('equal', adjustable='box')
plt.tight_layout()
plt.show()
def visualize(self):
df_dx_zz = self.compute_partial_derivative(x)
self.plot_partial_derivative('x', df_dx_zz)
df_dy_zz = self.compute_partial_derivative(y)
self.plot_partial_derivative('y', df_dy_zz)
visualization = FunctionVisualization()
visualization.visualize()
通过上述代码,我们可以清晰地看到函数f(x, y) = sin(x) * cos(y)及其关于x和y的偏导数的三维图和二维等高线图。这种可视化方法有助于我们更直观地理解偏导数的概念和函数的变化趋势。
热门推荐
NASA揭示人类活动对地球水循环的深远影响
“籍贯”是什么?从古代到现代的身份印记
苹果市值飙升:3.62万亿历史新高
探索天龙八部的世界 - 结识传奇英雄,体验武林
三部探讨人性与社会的末日题材美剧推荐
深度游,玩转香港新体验
穿越千年智慧:解读《论语·述而篇》的人生哲学
Web设计中如何选取合适的图片?
生产经营单位如何开展安全培训?
黄种人概念背后的种族观念与文化认同探析
田氏代齐之后国名未改的背后:权力更迭与文化传承的交融
农村自建房窗户风格全解析
碳酸锂的至暗时刻:2024年价格屡创新低,成败皆因锂矿
游泳减肥的正确方法 每周至少游泳3-4次
深度学习:5种经典神经网络模型介绍
全球十大经典车标logo设计及其深层含义解读
知名作家的4个写作技巧,条条干货,值得收藏
AI技术助力,驻马店市优化政务服务新体验
调味品的秘密:胡椒品种、功效、禁忌一次看!
狗狗的寿命究竟有多长?(探索影响狗狗寿命的因素和关键决定)
大型商场为何纷纷变身“美食城”?这场转型背后藏着怎样的商业逻辑
数字教学知识库:构建高效学习生态的基石
固态电池概念股回暖,金发科技涨停如何影响市场?
黑芝麻糊热量是多少 一碗黑芝麻等于几碗饭
高龄患者为什么建议做三代试管婴儿?
女性反诈指南,帮你避“坑”
Vue中动态设置按钮readonly属性的多种方法
眼前有絮状物飘动是怎么回事
猪肋骨数目探秘
农行申请减免利息需要哪些步骤?