title: Why does Scipy.misc.derivative give wrong answer Python
body: <p>I try to plot the derivative of a piecewise function.</p>
  
  <p><img src="https://i.stack.imgur.com/7VXS1.png" alt=""></p>
  
  <p>I worked out the math the derivative exists everything should be fine and I want to see what happen at the "singularity", but I get:</p>
  
  <p>singularity at 'x=0'</p>
  
  <p><img src="https://i.stack.imgur.com/XSTeV.png" alt=""></p>
  
  <p>which opposes to what I expect:</p>
  
  <p>The "kind of" result I expect</p>
  
  <p><img src="https://i.stack.imgur.com/zSxIA.png" alt=""></p>
  
  <p>My code:</p>
  
  <pre><code>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
  </code></pre>