#include "TrapeziodalIntegration.h" #include "myFunction.h" double TrapezoidalArea(double startX, double endX, int nSubIntervals) { double dx = (endX - startX)/nSubIntervals; double yi[nSubIntervals + 1]; for(int index=0; index<=nSubIntervals; index++) yi[index] = myFunction(startX + index*dx); double sum = yi[0] / 2.0; // initialize sum to 1/2 of f(startX) for(int j = 1; j < nSubIntervals; j++) sum += yi[j]; // add f(xi) to sum sum += yi[nSubIntervals] / 2.0; // add 1/2 of f(endX) to sum return dx * sum; // total area of trapezoids }