Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lettergram/fa41959d825281a9e231 to your computer and use it in GitHub Desktop.
import math
def comp_simpson(f, a, b, n):
h = (b - a) / n
sum = f(a) + f(b)
for i in range(1, n, 2):
sum += 4 * f(a + i * h)
for i in range(2, n-1, 2):
sum += 2 * f(a + i * h)
return sum * h / 3
print comp_simpson(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