# Demonstration code for Scientific Computing class, # http://www.math.nyu.edu/faculty/goodman/teaching/SciComp2022/ # IntegrationDemo.py # Demonstrate a software package, with IntegrandClass.py # and IntegrationPackage.py import IntegrationPackage as ip # Code to do numerical integration import IntegrandClass as ic # How the integrand is defined import numpy as np # to use numpy arrays # use the package to integrate a polynomial d = 2 # a quadratic polynomial c = np.zeros(d+1) # an array for the coefficients in f(x) c[0] = 1. # f(x) = 1+2x+3x^3 c[1] = 2. c[2] = 3. poly = ic.Integrand(d, c) # a polynomial class instance with these coefficients x = 0. outString = "Integrand: f(x) with x ={x:6.2f} is f ={f:6.2f}" outString = outString.format(x = x, f = poly.f(x) ) print(outString) x = 2. outString = "Integrand: f(x) with x ={x:6.2f} is f ={f:6.2f}" outString = outString.format(x = x, f = poly.f(x) ) print(outString) n = 1000 ans = ip.Integrate( poly, -1., 1., n) outString = "Approximate integral using {n:5d} points is {ans:8.4f}" outString = outString.format(n = n, ans = ans) print(outString) n = 10000 ans = ip.Integrate( poly, -1., 1., n) outString = "Approximate integral using {n:5d} points is {ans:8.4f}" outString = outString.format(n = n, ans = ans) print(outString)