Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active August 29, 2015 13:56
Show Gist options
  • Save jesuscast/9295885 to your computer and use it in GitHub Desktop.
Save jesuscast/9295885 to your computer and use it in GitHub Desktop.
Calculates simpson's sum, receives the interval and the the number of intervals. An extra y function needs to be created separetely. Uses numpy. This function takes the results of the y function
from numpy import *
def simpsonSum(xRange,n):
tot = 0
width = 0
width = (xRange[1]-xRange[0])/n
x = [ interval for interval in arange(xRange[0],xRange[1]+width, width) ]
yRange = [ y(n) for n in x ]
tot += yRange[0]+yRange[n]
tot += sum([ 2*yRange[i] if (i%2==0) else 4*yRange[i] for i in range(1,n) ])
tot = tot*width/3
return tot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment