#include #include #include #include double f(double x); double antider_f(double x); double intergal_rect( double (*f)(double), double a, double b, double h = 0.001 ); double intergal_trap( double (*f)(double), double a, double b, double h = 0.001 ); double intergal_simpson( double (*f)(double), double a, double b, double h = 0.001 ); int main() { while (true) { std::cout << "Enter a, b:" << std::endl; double a, b; std::cin >> a >> b; if (!std::cin.good()) break; std::cout << "Enter step size h:" << std::endl; double h; std::cin >> h; double int_rect = intergal_rect(&f, a, b, h); double integral = antider_f(b) - antider_f(a); std::cout << std::fixed << std::setprecision(14); std::cout << "Integral (rectangles) = " << int_rect << std::endl; std::cout << "Integral (presize) = " << integral << std::endl; std::cout << "Error: " << fabs(int_rect - integral) << std::endl; double int_trap = intergal_trap(&f, a, b, h); std::cout << "Integral (trap.) = " << int_trap << std::endl; std::cout << "Integral (presize) = " << integral << std::endl; std::cout << "Error: " << fabs(int_trap - integral) << std::endl; } return 0; } double f(double x) { return x*x*x; } double antider_f(double x) { return x*x*x*x/4.; } double intergal_rect( double (*f)(double), double a, double b, double h /* = 0.001 */ ) { double factor = 1.; if (b < a) { std::swap(a, b); factor = (-1.); } int n = int((b - a)/h); if (n == 0) n = 1; double dx = (b - a)/n; double s = 0.; double x = a; for (int i = 0; i < n; ++i) { s += (*f)(x); x += dx; } s *= dx; return s*factor; } double intergal_trap( double (*f)(double), double a, double b, double h /* = 0.001 */ ) { // To do... return 0.; } double intergal_simpson( double (*f)(double), double a, double b, double h /* = 0.001 */ ) { // To do... return 0.; }