Last active
August 29, 2015 13:56
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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