Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/6fe66b5565e5e1372d5b to your computer and use it in GitHub Desktop.
Save lettergram/6fe66b5565e5e1372d5b to your computer and use it in GitHub Desktop.
import math
def comp_trap(f, a, b, n):
h = (b - a) / n
sum = f(a) / 2
for i in xrange(1, n):
sum += 2 * f(a + i * h)
return sum * h / 2
print comp_trap(lambda x:math.sin(x), 0.0, math.pi / 2, 120)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment