/* calculation of integral trapezoidal method*/ #include #include #include using namespace std; double f(double x) {return x - x*x + x*x*x - x*x*x*x + sin(x*13.)/13.;} double fint(double x) {return x*x/2. - x*x*x/3. + x*x*x*x/4. - x*x*x*x*x/5. - cos(x*13.)/169.;} int main() { double fint_exact = fint(1.2)-fint(0.); double area = 0., h = 1E-4, x = 0.; double f0,f1,f2 = f(x); while(x<1.2-h*1.5) { f0 = f2; x += h; f1 = f(x); x += h; f2 = f(x); area += (f0+f1*4.+f2); } area *= (h/3.); cout << "Exact: " << setprecision(18) << fint_exact << " Numerical: " << setprecision(18) << area << " diff: " << fabs(fint_exact-area) << endl; }