Numerical integration: Difference between revisions

→‎{{header|Python}}: Do all four tests using floats and fractions
(A little more precision on the ln(100). P.S. Do we need the other two tests?)
(→‎{{header|Python}}: Do all four tests using floats and fractions)
Line 1,526:
def reciprocal(x):
return 1/x
def identity(x):
return x
def integrate( f, a, b, steps, meth):
h = (b-a)/steps
ival = h * sum(meth(f, a+i*h, h) for i in range(steps))
return ival</lang>
 
'''Tests'''
# Trial
<lang python>for a, b, steps, func in ((0., 1., 100, cube), (1., 100., 1000, reciprocal)):
for rule in (left_rect, mid_rect, right_rect, trapezium, simpson):
print('%s integrated using %s\n from %r to %r (%i steps) = %r' %
(func.__name__, rule.__name__, a, b, steps,
integrate( func, a, b, steps, rule)))
a, b = Fraction.from_float(a), Fraction.from_float(b)
for rule in (left_rect, mid_rect, right_rect, trapezium, simpson):
print('%s integrated using %s\n from %r to %r (%i steps and fractions) = %r' %
(func.__name__, rule.__name__, a, b, steps,
float(integrate( func, a, b, steps, rule))))
 
# Extra tests (compute intensive)
for a, b, steps, func in ((1., 5000., 5000000, identity),
(1., 6000., 6000000, identity)):
for rule in (left_rect, mid_rect, right_rect, trapezium, simpson):
print('%s integrated using %s\n from %r to %r (%i steps) = %r' %
Line 1,544 ⟶ 1,560:
float(integrate( func, a, b, steps, rule))))</lang>
 
'''Sample test Output'''
<pre>cube integrated using left_rect
from 0.0 to 1.0 (100 steps) = 0.24502500000000005
Line 1,584 ⟶ 1,600:
from Fraction(1, 1) to Fraction(100, 1) (1000 steps and fractions) = 4.605986057514677
reciprocal integrated using simpson
from Fraction(1, 1) to Fraction(100, 1) (1000 steps and fractions) = 4.605170384957134</pre>
identity integrated using left_rect
from 1.0 to 5000.0 (5000000 steps) = 12499997.0009999
identity integrated using mid_rect
from 1.0 to 5000.0 (5000000 steps) = 12499999.499999998
identity integrated using right_rect
from 1.0 to 5000.0 (5000000 steps) = 12500001.999000099
identity integrated using trapezium
from 1.0 to 5000.0 (5000000 steps) = 12499999.499999998
identity integrated using simpson
from 1.0 to 5000.0 (5000000 steps) = 12499999.499999998
identity integrated using left_rect
from Fraction(1, 1) to Fraction(5000, 1) (5000000 steps and fractions) = 12499997.0009999
identity integrated using mid_rect
from Fraction(1, 1) to Fraction(5000, 1) (5000000 steps and fractions) = 12499999.5
identity integrated using right_rect
from Fraction(1, 1) to Fraction(5000, 1) (5000000 steps and fractions) = 12500001.9990001
identity integrated using trapezium
from Fraction(1, 1) to Fraction(5000, 1) (5000000 steps and fractions) = 12499999.499999998
identity integrated using simpson
from Fraction(1, 1) to Fraction(5000, 1) (5000000 steps and fractions) = 12499999.499999998
identity integrated using left_rect
from 1.0 to 6000.0 (6000000 steps) = 17999996.50099991
identity integrated using mid_rect
from 1.0 to 6000.0 (6000000 steps) = 17999999.500000004
identity integrated using right_rect
from 1.0 to 6000.0 (6000000 steps) = 18000002.499000076
identity integrated using trapezium
from 1.0 to 6000.0 (6000000 steps) = 17999999.500000004
identity integrated using simpson
from 1.0 to 6000.0 (6000000 steps) = 17999999.500000004
identity integrated using left_rect
from Fraction(1, 1) to Fraction(6000, 1) (6000000 steps and fractions) = 17999996.500999916
identity integrated using mid_rect
from Fraction(1, 1) to Fraction(6000, 1) (6000000 steps and fractions) = 17999999.5
identity integrated using right_rect
from Fraction(1, 1) to Fraction(6000, 1) (6000000 steps and fractions) = 18000002.499000084
identity integrated using trapezium
from Fraction(1, 1) to Fraction(6000, 1) (6000000 steps and fractions) = 17999999.500000004
identity integrated using simpson
from Fraction(1, 1) to Fraction(6000, 1) (6000000 steps and fractions) = 17999999.500000004</pre>
 
A faster Simpson's rule integrator is
Anonymous user