Question: Using Python 2 the 2 functions referenced def midpoint_integration(f, a, b, n=100): h = (b - a)/float(n) I = 0 for i in range(n): I


def midpoint_integration(f, a, b, n=100): h = (b - a)/float(n) I = 0 for i in range(n): I += f(a + i*h = 0.5*h) return h*I 8 9 10 11 12 13 14 15 16 from math import * import sys f_formula = sys.argv[1] a = eval(sys.argv[2]) b = eval(sys.argv[3]) if len(sys.argv) >= 5: n = int(sys.argv[4]) else: n = 200 17 18 19 20 code = " def g(x): return $s % f_formula exec(code) 21 22 23 24 25 I = midpoint integration(g, a, b, n) print 'Integral of %s on %, *g) with n=d: %' % (f_formula, a, b, n, I) useful, we have made a special tool that hides the technicalities. The tool is named StringFunction and works as follows: >>> from scitools.StringFunction import StringFunction >>> formula = exp(x)*sin(x)' >>> f = StringFunction(formula) # turn formula into f(x) func
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
