我试着画一个分段函数的导数。在
{1美元^
我算出了导数存在的数学,一切都应该是好的,我想看看在“奇点”发生了什么,但我得到:
“x=0”处的奇点
与我的期望相反:
我期望的那种结果
我的代码:from scipy.misc import derivative as deriv
import numpy as np
import pylab as pyl
def f(x): # Define piecewise function
if x != 0:
h = np.power(x, -1)
return x**2 * np.sin(h)
elif x == 0:
return 0
vf = np.vectorize(f) # Vectorize function to use "deriv"
x = np.linspace(-1, 1, num=10 ** 5) # Make 'x' continuous parameter
x = np.sort(np.append(x, [0])) # Make sure 'x' contains '0'
def d(x): return deriv(vf, x) # Define derivative of 'f' respect to 'x'
print('0, ' + str(d(0))) # Derivative at '0'
pyl.plot(x, vf(x), 'b-') # Plot functions
pyl.plot(x, d(x), 'C4')
pyl.scatter(0, d(0), c='r0')
pyl.grid()
pyl.show() # Display graphically