Advertisement
adamnoeva

Untitled

May 26th, 2022
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double function(double x);
  7. void inputdata(int& a, int& b, int& n, double& eps);
  8. double middle(int a, int b, int n);
  9. double tropetion(int a, int b, int n);
  10. double Simpson(int a,int b,int n);
  11. double automatic_1(int a, int b, double eps);
  12. double automatic_2(int a, int b, double eps);
  13.  
  14. int main()
  15. {
  16.     int a, b, n;
  17.     double eps = 0;
  18.     setlocale(LC_ALL, "ru");
  19.     cout << "Функция: 10) x^3-5x^2" << endl;
  20.     inputdata(a, b, n, eps);
  21.     cout << endl << endl;
  22.  
  23.     cout << "Результат средних: " << middle(a, b, n) << endl << "Результат трапеций: " << tropetion(a, b, n) << endl <<
  24.         "Результат Симпсон: " << Simpson(a, b, n) << endl;
  25.  
  26.     cout << "Результат автомат_1: " << automatic_1(a, b, eps) << endl << "Результат автомат_2: " << automatic_2(a, b, eps);
  27.  
  28. }
  29.  
  30. double function(double x)
  31. {
  32.     return x * x * x - 5 * x * x;
  33. }
  34.  
  35. void inputdata(int& a, int& b, int& n, double& eps)
  36. {
  37.     cout << "Начало интервала: ";
  38.     cin >> a;
  39.     do
  40.     {
  41.         cout << "Введите конец интервала: ";
  42.         cin >> b;
  43.     } while (b < a);
  44.     do
  45.     {
  46.         cout << "Введите число разбиений: ";
  47.         cin >> n;
  48.     } while (n <= 0);
  49.     do
  50.     {
  51.         cout << "Введите точность: ";
  52.         cin >> eps;
  53.     } while (eps <= 0);
  54. }
  55.  
  56. double tropetion(int a, int b, int n)
  57. {
  58.     double result = 0;
  59.     double h = (double)(b - a) / n;
  60.     for (double x = a; x <= b; x += h)
  61.     {
  62.         result += (function(x) + function(x+h)) / 2;
  63.     }
  64.     return result * h;
  65. }
  66.  
  67. double Simpson(int a, int b, int n)
  68. {
  69.     double result = 0;
  70.     double h = (double)(b - a) / n;
  71.     double x = a;
  72.     while(x<=b)
  73.     {
  74.         result += function(x) + 4 * function(x + h / 2) + function(x + h);
  75.         x += h;
  76.     }
  77.     return result * h / 6;
  78. }
  79.  
  80. double automatic_1(int a, int b, double eps)
  81. {
  82.     int n = 1;
  83.     while (fabs(Simpson(a, b, n) - Simpson(a, b, n * 2)) >= eps)
  84.         n *= 2;
  85.     return Simpson(a, b, n * 2);
  86. }
  87.  
  88. double automatic_2(int a, int b, double eps)
  89. {
  90.     int n = 1;
  91.     while (fabs(tropetion(a, b, n) - middle(a, b, n)) >= eps)
  92.         n *= 2;
  93.     return (tropetion(a, b, n) + 2 * middle(a, b, n)) / 3;
  94. }
  95.  
  96. double middle(int a, int b, int n)
  97. {
  98.     double result = 0;
  99.     double h = (double)(b - a) / n;
  100.     for (double x = a + h / 2; x <= b; x += h)
  101.     {
  102.         result += function(x);
  103.     }
  104.     return result * h;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement