# Demonstration code for Scientific Computing class, # http://www.math.nyu.edu/faculty/goodman/teaching/SciComp2022/ # IntegrandClass.py # Demonstrate a software package, with IntegrandDemo.py # and IntegrandClass.py # Integrate a function over [a,b] using the rectangle rule def Integrate( f, a, b, n): """Use the rectangle rule to integrate from a to b using n points f is a class instance with f.f(x) being the value of the integrand at x """ I = 0. # will be the integral dx = (b-a)/n # distance between grid points for k in range(n): xk = a + k*dx I = I + f.f(xk)*dx return I