
在验证立方根函数可导性的过程中,我意外掉进了数值计算的深坑。本文将记录从错误可视化到正确分析的全过程,特别强调使用不同计算方法的图像差异。
当我们需要验证函数f(x) = x^(1/3)在x=0处的可导性时,直观的思路是:
但在实际编码中,第一步就栽了跟头——绘制出的图像竟不是真正的立方根曲线!
from __future__ import annotations , annotations
from fractions import Fraction
import matplotlib.pyplot as plt
import numpy as np
# ==================== 1. 数据生成 ====================
x_begin = -10
x_end = 10
numbers_count = 5000
y_begin = -2.5
y_end = 2.5
title = 'x^(1/3)'
curve_name = 'x^(1/3)'
x_label = 'x-axis'
y_label = 'y-axis'
x = np.linspace(x_begin , x_end , numbers_count)
# 生成多个 y 函数(可根据需要添加/修改)
# 同时定义每个函数曲线的绘图风格
y_functions = {
curve_name:{
'data': xxxxxxxxx, # 实际函数计算的逻辑
'style':{ 'color':'blue' , 'linestyle':'-' , 'linewidth':1 } ,
} ,
}
# ==================== 高亮标记配置 ====================
# 情况1:直接指定 (x, y) 坐标点
highlight_points = [
(0 , 0) ,
(1 , 1) ,
(-1 , -1) ,
]
# ==================== 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()*在上述代码模板中,每次绘图时需要设置的参数如下图,
其中黄色框里面的代码片段是本次针对立方根函数绘图时需要特别注意的地方。

x**(1/3)
问题表现:

这张图像中:
原因分析:
Python在处理负数分数指数时:

问题表现:

对每个x<0的值都抛出异常:
RuntimeWarning: invalid value encountered in power 'data': pow(x , 1 / 3) ,
原因分析:pow无法处理负数的分数次幂

效果:

图中清晰显示:
x**(1/3)仅绘制了x≥0的部分曲线math.pow方法导致程序崩溃,无法绘图np.cbrt正确绘制整个实数域上的立方根曲线

import sympy as sp
symbol_x = sp.symbols('x')
expr = symbol_x ** Fraction(1 , 3)
expr_prime = sp.diff(expr , symbol_x)
print(f'{sp.latex(expr)} 的导数函数是: {sp.latex(expr_prime)}')
right_limit = sp.limit(expr_prime , symbol_x , 0 , '+')
print(f'导数函数在 0 处的右极限为: {right_limit}')
left_limit = sp.limit(expr_prime , symbol_x , 0 , '-')
print(f'导数函数在 0 处的左极限为: {left_limit}')
zero_limit = sp.limit(expr_prime , symbol_x , 0 , '+-')
print(f'导数函数在 0 处于的极限为: {zero_limit}')
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。