问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

偏导数的可视化:以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的偏导数的三维图和二维等高线图。这种可视化方法有助于我们更直观地理解偏导数的概念和函数的变化趋势。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号