/* Numerical integration of f(x) on [a,b] using 3 following methodss: # Trapezoid approximation # Simpson's rule # 8-panel newton-cotes rule (quanc8.cpp) time of integration is calculated and printed AG - Februaru 2007 */ #include #include #include #include #include #include "trapezoid.cpp" #include "simpson.cpp" #include "quanc8.cpp" using namespace std; double f(double); //function prototype int main() { double a, b, trap, simp; double nc8, errest, flag; int i, n, nofun; int ntimes; const double pi = 3.1415926; const double abserr=0.0, relerr=1.0e-10; //see quanc8.cpp cout.precision(6); cout.setf(ios::fixed | ios::showpoint); // current time in seconds (begin calculations) time_t seconds_i; seconds_i = time (NULL); /* initial information */ a = 0.0; // left endpoint b = pi; // right endpoint n = 2; // initial number of intervals ntimes = 12; // number of interval doublings with nmax=2^(ntimes) cout << " Intervals "<<"Trapez. "<<"Simpson "<< endl; /* step 2: integration with trapezoid and simpson */ for (i=1; i <=ntimes; i=i+1) { trap = trapezoid(f, a, b, n); simp = simpson(f, a, b, n); cout << setw(10) << n << setw(10) << trap << setw(10) << simp <