title: Calculating definite integral of array
body: <p>I need to compute the definite integral of an equation represented by an array of its y-values and an array of its x-values.</p>
  
  <p>I was originally going to use np.trapz, but then I couldn't specify the bin size I wanted to integrate over, so I wrote my own function that takes in the x-array, y-array, lower and upper bounds of integration, and step/bin size.</p>
  
  <pre class="lang-py prettyprint-override"><code>def integrate(fx, fy, lower_bound, upper_bound, step):
  
      # sort it, and then trim off any elements that are out of bounds
      output_x = np.sort(fx)
      output_x = output_x[lower_bound : upper_bound]
  
      output_y = np.zeros(len(output_x))
  
      # loop over every x value in output_x
      for i in range(len(output_x)):
          for j in range(len(output_y)):
              # interpolate the values
              interpolated = np.interp(output_x[i], fx, fy)
              # calculate integral
              q = (interpolated[j] - interpolated[j-1]) * step
              result = np.sum(q)
  
  
      return (result)
  
  
  array_x = np.arange(0,10,.2)
  array_y = np.sqrt(array_x)
  
  integrate(array_x, array_y, .125, 8, .25)
  </code></pre>
  
  <p>My expected output is just the value of the definite integral. I'm getting the following error:</p>
  
  <blockquote>
    <p>TypeError                                 Traceback (most recent call last)
     in ()
          1 ################ Plutonium 239
          2 
    ----> 3 integrate(mult_x2, mult_y2, .125, 8, .25)</p>
    
    <p> in integrate(fx, fy, lower_bound, upper_bound, step)
          7     # we sort it, and then trim off any elements that are out of bounds
          8     output_x = np.sort(fx)
    ----> 9     output_x = output_x[lower_bound : upper_bound]
         10 
         11     output_y = np.zeros(len(output_x))</p>
    
    <p>TypeError: slice indices must be integers or None or have an <strong>index</strong> method</p>
  </blockquote>