title: Integration using Newton-Cotes method in python
body: <p>Im trying to write a function in python that will integrate a given function using the Newton-Cotes method, and its returning some extremely strange results, where it sometimes gives the correct answer and others not (mostly wrong with -ve bounds). This is my code, if anyone could point out any mistakes it would be greatly appreciated :)</p>
  
  <pre><code>def integrate(function, a, b):
      coeff = [7,32,12,32,7]
      result = 0
      for i in range(0,len(coeff)):
          x = a + (i*(b-a))/(len(coeff)-1)
          result += coeff[i]*eval(function)
          print eval(function)
      result = result*((b-a)/90.)
      return result
  </code></pre>
  
  <p>The newton cotes formula I've been following is from <a href="http://en.wikipedia.org/wiki/Newton-Cotes_formulas" rel="nofollow">wikipedia</a>.</p>
  
  <p>Example:</p>
  
  <pre><code>print integrate("x**3-4*x+9", -7, 7)
  </code></pre>
  
  <p>Returns: <code>-38</code> when the actual answer is <code>126</code></p>