Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (49 sloc) 1.48 KB
"""Module for integrating functions by the Midpoint rule."""
from math import *
import sys
def integrate(f, a, b, n):
"""Return the integral of f from a to b with n intervals."""
h = float(b-a)/n
s = 0
for i in range(1, n+1):
s += f(a + (i-0.5)*h)
return s*h
def test_integrate():
"""Check that linear functions are integrated exactly."""
def g(x):
return p*x + q # general linear function
def int_g_exact(x): # integral of g(x)
return 0.5*p*x**2 + q*x
a = -1.2; b = 2.8 # "arbitrary" integration limits
p = -2; q = 10
success = True # True if all tests below are passed
for n in 1, 10, 100:
I = integrate(g, a, b, n)
I_exact = int_g_exact(b) - int_g_exact(a)
error = abs(I_exact - I)
if error > 1E-14:
success = False
assert success
def main():
"""
Read f-formula, a, b, n from the command line.
Print the result of integrate(f, a, b, n).
"""
try:
f_formula = sys.argv[1]
a = eval(sys.argv[2])
b = eval(sys.argv[3])
n = int(sys.argv[4])
except IndexError:
print 'Usage: %s f-formula a b n' % sys.argv[0]
sys.exit(1)
from scitools.std import StringFunction
f = StringFunction(f_formula)
I = integrate(f, a, b, n)
print I
if __name__ == '__main__':
if sys.argv[1] == 'verify':
verify()
else:
# Compute the integral specified on the command line
main()