Method of Exhaustion is a generalization of Archimedes Method for calculating PI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Numerical Integration using Method of Exhaustion
# http://en.wikipedia.org/wiki/Method_of_exhaustion
# FB - 201012212
import math
def integ(fun, a, b, maxIt):
ba = b - a
s = 0.0
n2 = 1
for n in range(1, maxIt):
sgn = 1
n2 *= 2
for m in range(1, n2):
s += (sgn * fun(a + m * ba / n2) / n2)
sgn = -sgn
return s * ba
print integ(lambda x: math.sqrt(4.0 - x * x), 0.0, 2.0, 20)
|
Tags: math, mathematics