title: Python - "Index Error" when I run program
body: <p>I'm writing code for taking the second derivative but I'm getting an error. It's saying there's something wrong with the second derivative equation line. I don't understand what I'm going wrong.</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  def d2(f,dx):
      df_dx = []
      for i in range(len(f)-1):
          df_dx.append(f[i+dx] - 2*f[i] + f[i-dx])/(dx*dx)
  
  f = np.sin(np.linspace(0,5,50))
  output = d2(f,0.1)
  expected = np.cos(np.linspace(0,4.9,49))
  diff = abs(output-expected)
  plt.plot(diff)
  </code></pre>
  
  <p>IndexError: only integers, slices (<code>:</code>), ellipsis (<code>...</code>), numpy.newaxis (<code>None</code>) and integer or boolean arrays are valid indices</p>