/* Jonathan Hauenstein - University of Notre Dame Center for Applied Mathematics Minicourse on Parallel Computing April 24, May 1, May 8, 2008 Sample programs are designed for educational use */ #include "trapezoid.h" #include double f(double x) { // function to evaluate if (-1 <= x && x <= 1) return sqrt(1 - x*x); else return 0; } double trapApprox(double a, double b) { // computes and returns the trapezoid integral approximation return 0.5*(b - a)*(f(a) + f(b)); } double composite_trapApprox(trapInfo *info) { // computes and returns the composite trapezoid integral approximation // Note: this is not the most efficient implementation since it is designed for educational purposes. int i; double stepSize, approx = 0; if (info->n > 0) { // compute the step size stepSize = (info->b - info->a) / info->n; // compute the approximation for (i = 0; i < info->n; i++) approx += trapApprox(info->a + i*stepSize, info->a + (i+1)*stepSize); } return approx; }