Advertisement
Technoblade777

Untitled

May 30th, 2023
894
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. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. double Analytical(double x)
  9. {
  10.     if(x==0){return 1/3;}
  11.     else{return ((2*x*x+2*x)*sin(x)+(-1*pow(x, 4)-2*pow(x, 3)-x*x+2)*cos(x)+x*x*x*x-2)/pow(x, 3);}
  12.  
  13. }
  14.  
  15. double Function(double x, double t)
  16. {
  17.     return pow(t+x, 2)*sin(x*t);
  18. }
  19.  
  20. double Right_rect(double x, double n, double h,double a)
  21. {
  22.     double sum = 0;
  23.     for (int i = 1; i <= n; i++)
  24.  
  25.         sum += h*(Function(x, a + h * i));
  26.     return sum;
  27. }
  28.  
  29. int main()
  30. {
  31.     setlocale(LC_CTYPE, "Russian");
  32.  
  33.     const double PI = 3.141592653589793;
  34.  
  35.     double a = 0, b = PI, c, d, N[3], difference[3] = {0, 0, 0};
  36.  
  37.     cout << "Введите нижнюю границу для х " << endl;
  38.     cin >> c;
  39.     cout << "Введите верхнюю границу для х " << endl;
  40.     cin >> d;
  41.     if(c<0||d>PI){cout << "Выход за область определениея"; return 0;}
  42.     cout << "Введите количество разбиений - 3 разных значения: " << endl;
  43.     cin >> N[0] >> N[1] >> N[2];
  44.  
  45.     fstream fin("/home/daminator/Desktop/Education/KUBSU/Programming methods/Practise/Практика летняя/output.txt");
  46.     fin << "x \t\t"  << "N="<<N[0] << "\t\t" << "N=" << N[1] << "\t\t" << "N=" << N[2] << "\t\t" << "Analytical" << '\n';
  47.     for (int i = 0; i <= 20; i++)
  48.     {
  49.         double x = c + i * (d - c) / 20;
  50.         double Analytic_Integral = Analytical(x);
  51.         fin << to_string(x) + '\t';
  52.         for (int j = 0; j < 3; j++)
  53.         {
  54.             double h = (b - a) / N[j];
  55.             double Rectangle_Integral = Right_rect(x, N[j], h, a);
  56.             fin << to_string(Rectangle_Integral) + '\t';
  57.             if (abs(Analytic_Integral - Rectangle_Integral) > difference[j])
  58.                 difference[j] = abs(Analytic_Integral - Rectangle_Integral);
  59.         }
  60.         fin << to_string(Analytic_Integral) + '\n';
  61.  
  62.     }
  63.     fin << "Для различных значений разбиений максимальная невязка  равна: \n";
  64.  
  65.     for (int i = 0; i < 3; i++)
  66.         fin << N[i] << ": " << difference[i] << "\n";
  67.  
  68.     fin.seekg(0);
  69.  
  70.     string getcontent;
  71.  
  72.     if (fin.is_open())
  73.         while (getline(fin, getcontent))
  74.             cout << getcontent << endl;
  75.  
  76.     fin.close();
  77.     return 0;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement