Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active August 29, 2015 13:56
Show Gist options
  • Save jesuscast/9296095 to your computer and use it in GitHub Desktop.
Save jesuscast/9296095 to your computer and use it in GitHub Desktop.
Calculate the trapezoidal sum of a function over an interval. An external y function needs to be created. Uses numpy
def trapezoidalSum(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] for i in range(1,n) ])
tot = tot*width/2
return tot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment