首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >以单变量函数为例进行导数定义的验证与计算

以单变量函数为例进行导数定义的验证与计算

原创
作者头像
密码学人CipherHUB
发布2025-06-01 21:43:05
发布2025-06-01 21:43:05
19300
代码可运行
举报
文章被收录于专栏:数理视界数理视界
运行总次数:0
代码可运行

导数的定义与理解

在微积分中,导数描述的是函数在某一点处的瞬时变化率。其核心定义是一个极限过程:

对于单变量函数 ( f(x) ),在点 ( x = a ) 处的导数定义为:

这个定义强调两个关键点:

  1. 逼近思想:通过自变量微小变化(h->0)观察函数值变化
  2. 变化率计算:函数值变化量除以自变量变化量

本文将以函数 x*sin(x) 在点 x = pi 处为例,展示如何用Python的SymPy库验证导数定义。

Python验证

代码语言:python
代码运行次数:0
运行
复制
from __future__ import annotations

from fractions import Fraction

import sympy as sy

"""
导数的英文标准翻译是 derivative
    f(x) 的导数 -> derivative of f(x)
    f 在 x = a 处的导数 -> derivative of f at x equals a
    dy/dx (导数的莱布尼茨记号) -> dy dx (读音: “dee y by dee x”)
    求导 -> differentiation
    导数 (作为一个数学概念) -> derivative
"""

sy.init_printing(use_latex = True , use_unicode = True)

"""
验证单变量函数中导数(微分方程)的定义
"""
# 定义单变量函数 f(x) = sin(x)*x
param_x = sy.Symbol('x')
expr = sy.sin(param_x) * param_x
print(f'原函数 = {expr}, latex: {sy.latex(expr)}')
# 函数 x*sin(x) 的导数: x*cos(x) + sin(x)
derivative = sy.diff(expr , param_x)
print(f'导函数 = {derivative}, latex: {sy.latex(derivative)}')
# 首先直接求导数在 pi 处的极限
limit_at_pi_right = sy.limit(derivative , param_x , sy.pi , '+')  # 右极限
limit_at_pi_left = sy.limit(derivative , param_x , sy.pi , '-')  # 左极限
limit_at_pi = sy.limit(derivative , param_x , sy.pi , '+-')  # 极限
print(f'导数在 pi 处的右极限:{limit_at_pi_right}, 左极限:{limit_at_pi_left}, 极限值: {limit_at_pi}')
print(f'在 pi 处连续且导数存在:{limit_at_pi_right == limit_at_pi_left == limit_at_pi}')
# 定义 x 轴的偏移量 h
param_h = sy.Symbol('h')
# 原函数加上 h 得到新函数 delta
delta = expr.subs(param_x , param_x + param_h)
print(f'原函数加上 h 得到新函数 delta:{delta}, {sy.latex(delta)}')
# delta 函数中将 x 替换成具体的值 pi,此时 delta 中只有一个变量 h
delta_pi = delta.subs(param_x , sy.pi)
print(f'delta 函数中将 x 替换成具体的值 pi,此时 delta 中只有一个变量 h:{delta_pi}, {sy.latex(delta_pi)}')
# 原函数将 x 替换成具体的值 pi
expr_pi = expr.subs(param_x , sy.pi)
print(f'原函数将 x 替换成具体的值 pi 得到的常量值:{expr_pi}')
# delta 在 pi 处减去原导数函数再比上 h,此时值为原函数在 pi 处的导数值
ret_limit = (delta_pi - expr_pi) / param_h
print(f'delta 在 pi 处减去原函数再比上 h,此时值为原函数在 pi 处的导数值:{ret_limit}, {sy.latex(ret_limit)}')
# 当 h 趋近于 0 时得到最终的导数值
ret = sy.limit(ret_limit , param_h , 0 , '+-')
print(f'当 h 趋近于 0 时得到最终的导数值:{ret}, '
      f'导数值存在且正确:{ret == limit_at_pi == limit_at_pi_right == limit_at_pi_left}')
导数验证结果
导数验证结果

绘制原函数与导数函数图像

代码语言:python
代码运行次数:0
运行
复制
from __future__ import annotations , annotations

import matplotlib.pyplot as plt
import numpy as np
import sympy as sy

# ==================== 1. 数据生成 ====================
# 生成 x 轴数据(示例:从 0 到 2π,均匀取 200 个点)
x_begin = -10 * np.pi
x_end = 10 * np.pi
numbers_count = 2000
y_begin = -32
y_end = 32
title = 'function derivative curve'
curve_name = 'x*sin(x)'
x_label = 'x-axis'
y_label = 'y-axis'
x = np.linspace(x_begin , x_end , numbers_count)

# 定义单变量函数 f(x) = sin(x)*x
param_x = sy.Symbol('x')
expr = sy.sin(param_x) * param_x
print(f'原函数 = {expr}, latex: {sy.latex(expr)}')

# 函数 x*sin(x) 的导数: x*cos(x) + sin(x)
derivative = sy.diff(expr , param_x)
print(f'导函数 = {derivative}, latex: {sy.latex(derivative)}')

# 生成多个 y 函数(可根据需要添加/修改)
# 同时定义每个函数曲线的绘图风格
y_functions = {
    curve_name:  {
        'data': [ expr.subs(param_x , i) for i in x ] ,
        'style':{ 'color':'blue' , 'linestyle':'-' , 'linewidth':1 } ,
        } ,
    'derivative':{
        'data': [ derivative.subs(param_x , i) for i in x ] ,
        'style':{ 'color':'red' , 'linestyle':'-' , 'linewidth':1 } ,
        } ,
    }

# ==================== 高亮标记配置 ====================
# 情况1:直接指定 (x, y) 坐标点
highlight_points = [
    (-np.pi / 2 , expr.subs(param_x , -sy.pi / 2)) ,
    (-np.pi , expr.subs(param_x , -sy.pi)) ,
    (-3 * np.pi / 2 , expr.subs(param_x , -3 * sy.pi / 2)) ,
    (-4 * np.pi / 2 , expr.subs(param_x , -4 * sy.pi / 2)) ,
    (0 , 0) ,
    (np.pi / 2 , expr.subs(param_x , sy.pi / 2)) ,
    (np.pi , expr.subs(param_x , sy.pi)) ,
    (3 * np.pi / 2 , expr.subs(param_x , 3 * sy.pi / 2)) ,
    (4 * np.pi / 2 , expr.subs(param_x , 4 * sy.pi / 2)) ,
    ]

# ==================== 2. 创建画布和坐标系 ====================
fig , ax = plt.subplots(figsize = (10 , 6) , dpi = 100)

# ==================== 3. 绘制多条曲线 ====================
for curve_func in y_functions:
    y = y_functions[ curve_func ][ 'data' ]
    if 'style' in y_functions[ curve_func ]:
        style = y_functions[ curve_func ][ 'style' ]
        ax.plot(x , y , label = curve_func , **style)
    else:
        ax.plot(x , y , label = curve_func)

# ==================== 4. 添加高亮标记 ====================
if highlight_points:
    # 相当于将列表展开为: (0, 0), (np.pi/2, 1)
    # zip( (0, 0), (np.pi/2, 1) ) 会按列对齐元素,重组数据
    # 得到迭代器:
    # ( (0, np.pi/2), (0, 1) )
    x_cords , y_cords = zip(*highlight_points)
    ax.scatter(
            x_cords , y_cords ,
            color = 'red' , marker = 'o' , s = 100 ,
            edgecolor = 'black' , zorder = 10 ,
            label = 'Specific Points' ,
            )

# ==================== 5. 添加图表元素 ====================
ax.legend(loc = 'upper right')
ax.set_title(title , fontsize = 14 , pad = 20)
ax.set_xlabel(x_label , fontsize = 12)
ax.set_ylabel(y_label , fontsize = 12)

# ==================== 6. 自定义样式 ====================
ax.grid(True , linestyle = '--' , alpha = 0.6)
ax.set_xlim(x_begin , x_end)
ax.set_ylim(y_begin , y_end)
ax.tick_params(axis = 'both' , labelsize = 10)

# ==================== 7. 显示/保存 ====================
plt.tight_layout()
plt.show()
导数函数曲线图
导数函数曲线图

更多函数的导数函数

代码语言:python
代码运行次数:0
运行
复制
x = sy.symbols('x')

# 函数的常数倍
expr_1 = 7 * (x ** 2) + 100
print(f'函数: {expr_1} 的导数函数为: {sy.diff(expr_1 , x)}, latex: {sy.latex(sy.diff(expr_1 , x))}')

# 函数和与函数差
expr_2 = 3 * (x ** 5) - 2 * (x ** 2) + 7 / sy.sqrt(x) + 2
print(f'函数: {expr_2} 的导数函数为: {sy.diff(expr_2 , x)}, latex: {sy.latex(sy.diff(expr_2 , x))}')

# 通过乘积法则求积函数的导数
expr_3 = (x ** 5 + 2 * x - 1) * (3 * x ** 8 - 2 * x ** 7 - x ** 4 - 3 * x)
print(f'函数: {expr_3} 的导数函数为: {sy.diff(expr_3 , x)}, latex:{sy.latex(sy.diff(expr_3 , x))}')

expr_4 = (x ** 3 + 2 * x) * (3 * x + sy.sqrt(x) + 1)
print(f'函数: {expr_4} {sy.latex(expr_4)} 的导数函数为: {sy.diff(expr_4 , x)}, latex: {sy.latex(sy.diff(expr_4 , x))}')

# 通过商法则求商函数的导数
expr_5 = (2 * x ** 3 - 3 * x + 1) / (x ** 5 - 8 * x ** 3 + 2)
print(f'函数: {expr_5} {sy.latex(expr_5)} 的导数函数为: {sy.diff(expr_5 , x)}, latex: {sy.latex(sy.diff(expr_5 , x))}')

# 通过链式求导法则求复合函数的导数
expr_6 = (x ** 2 + 1) ** 99
print(f'函数: {expr_6} {sy.latex(expr_6)} 的导数函数为: {sy.diff(expr_6 , x)}, latex: {sy.latex(sy.diff(expr_6 , x))}')

expr_7 = (3 * x ** 7 + x ** 4 * (sy.sqrt(2 * x ** 5 + 15 * x ** Fraction(4 , 3) - 23 * x + 9))) / 6 * x ** 2 - 4
print(f'函数: {expr_7} {sy.latex(expr_7)} 的导数函数为: {sy.diff(expr_7 , x)}, latex: {sy.latex(sy.diff(expr_7 , x))}')
expr_2
expr_2
expr_2 的导数函数
expr_2 的导数函数
expr3
expr3
expr_3 的导数函数
expr_3 的导数函数
expr4
expr4
expr_4 的导数函数
expr_4 的导数函数
expr5
expr5
expr_5 的导数函数
expr_5 的导数函数
expr6
expr6
expr_6 的导数函数
expr_6 的导数函数
expr7
expr7
expr_7 的导数函数
expr_7 的导数函数

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 导数的定义与理解
  • Python验证
  • 绘制原函数与导数函数图像
  • 更多函数的导数函数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档