第十四章 导数与偏导数(Derivative与Partial Derivative)

14.1 相关概念

       1、 数学计算的网站:Wolfram|Alpha: Computational Intelligence

        2、SymPy 是一个用于符号数学的 Python 库:SymPy

        导数(Derivative)的精髓在于“变化率”

        假设:

f(x)=x^2

        那么其导数为:

\frac{df(x)}{dx}=\frac{d(x^2)}{dx}=2x

         假设:

f(x,y)=x^2sin(x)+cos(xy)

        其偏导数为:

3D绘图 

 等高线图

14.2 通过Sympy库计算求导结果

# -*- coding: utf-8 -*-
"""
Created on Tue May 17 11:25:13 2022

@author: xiaofeng
"""

import sympy as sym
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d


def func(x, y):
    return x ** 2 * y + np.cos(y) + np.sin(x)


if __name__ == '__main__':
    sym.init_printing()
    x, y = sym.symbols("x y")
    f = x ** 2 * y + sym.cos(y) + sym.sin(x)
    # 只是显示数学公式,并不会得出求导结果(在notebook里就可以显示出标准的数学公式)
    sym.Derivative(f, x)
    # 可算出求导结果
    print(sym.diff(f, x))
    print(sym.diff(f, y))
    # 创建等差数列
    x = np.linspace(-888, 888, 8)
    y = np.linspace(-888, 888, 8)

    X, Y = np.meshgrid(x, y)

    Z = func(X, Y)

    fig = plt.figure(figsize=(15, 8))
    ax = plt.axes(projection="3d")
    ax.contour3D(X, Y, Z, 5, cmap="binary")
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap="viridis", edgecolor="none")
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("z")
    plt.show()

        运行结果为:

14.3 梯度(Gradient)

 function:f(x,y)

Gradient\;Vector:\nabla f(x,y)=\begin{bmatrix} \frac{\partial f(x,y)}{\partial x}\\ \\\frac{\partial f(x,y)}{\partial y}\end{bmatrix}=\begin{bmatrix} f_x\\\\f_y\end{bmatrix}

14.4 散度(Divergence)

         假设:

\vec{v}(x,y) = \begin{bmatrix} xy\\y^2-x^2\end{bmatrix}=\begin{bmatrix} P(x,y)\\Q(x,y)\end{bmatrix}

div\;\vec{v}=\frac{\partial{P}}{\partial{x}}+\frac{\partial{Q}}{\partial{y}} 

=y+2y 

 =3y

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年5月18日
下一篇 2022年5月18日

相关推荐