Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. typedef double (*ftype)(double, int);
  10.  
  11.  
  12. struct I_print {    //данные для печати результатов интегрирования
  13.     const char* name;//название функции
  14.     double i_sum;   //значение интегральной суммы
  15.     double i_toch;  //точное значение интеграла
  16.     int n;  //число разбиений области интегрирования
  17.             //при котором достигнута требуемая точность
  18. };
  19.  
  20.  
  21. void PrintTabl(I_print i_prn[], int k, char* gg);
  22.  
  23. double IntRect(ftype f, double a, double b, double eps, int& n, int mode);
  24.  
  25. double IntTrap(ftype f, double a, double b, double eps, int& n, int mode);
  26.  
  27. double i_toch(int mode);
  28.  
  29. double funcs(double x, int mode);
  30.  
  31. int main()
  32. {
  33.     double a = -1, b = 3;
  34.     int n = 0;
  35.     double eps = 0.1;
  36.     I_print i_prn[4];
  37.     const char* func_name[] = { "y=x ", "y=sin(22x)", "y=x^4 ", "y=arctg(x)" };
  38.     //IntRect
  39.     while (eps >= 0.000001) {
  40.  
  41.         cout << fixed << setw(30) << "eps = " << eps << endl;
  42.         for (int i = 0; i < 4; i++) {
  43.             I_print I_str = { func_name[i], IntRect(funcs, a, b, eps, n, i + 1), i_toch(i + 1), n };
  44.             i_prn[i] = I_str;
  45.         }
  46.         PrintTabl(i_prn, 4, "IntRect ");
  47.         eps /= 10;
  48.     }
  49.     cout << endl << endl;
  50.     eps = 0.1;
  51.     //IntTrap
  52.     while (eps >= 0.000001) {
  53.  
  54.         cout << fixed << setw(30) << "eps = " << eps << endl;
  55.         for (int i = 0; i < 4; i++) {
  56.             I_print I_str = { func_name[i], IntTrap(funcs, a, b, eps, n, i + 1), i_toch(i + 1), n };
  57.             i_prn[i] = I_str;
  58.         }
  59.         PrintTabl(i_prn, 4, "IntTrap ");
  60.         eps /= 10;
  61.     }
  62.     system("pause");
  63.     return 0;
  64. }
  65.  
  66. void PrintTabl(I_print i_prn[], int k, char* gg)
  67. {
  68.     const int m = 4;//число столбцов таблицы
  69.     int wn[m] = { 12,18,18,10 };//ширина столбцов таблицы
  70.     const char* title[m] = { "Function","Integral", gg ,"N " };
  71.     int size[m];
  72.     for (int i = 0; i < m; i++)
  73.         size[i] = strlen(title[i]);
  74.     //шапка таблицы
  75.     cout << char(218) << setfill(char(196));
  76.     for (int j = 0; j < m - 1; j++)
  77.         cout << setw(wn[j]) << char(194);
  78.     cout << setw(wn[m - 1]) << char(191) << endl;
  79.  
  80.     cout << char(179);
  81.     for (int j = 0; j < m; j++)
  82.         cout << setw((wn[j] - size[j]) / 2) << setfill(' ') << ' ' << title[j]
  83.         << setw((wn[j] - size[j]) / 2) << char(179);
  84.     cout << endl;
  85.     for (int i = 0; i < k; i++)
  86.     {//заполнение таблицы
  87.         cout << char(195) << fixed;
  88.         for (int j = 0; j < m - 1; j++)
  89.             cout << setfill(char(196)) << setw(wn[j]) << char(197);
  90.         cout << setw(wn[m - 1]) << char(180) << setfill(' ') << endl;
  91.  
  92.         cout << char(179) << setw((wn[0] - strlen(i_prn[i].name)) / 2) << ' ' << i_prn[i].name
  93.             << setw((wn[0] - strlen(i_prn[i].name)) / 2) << char(179);
  94.         cout << setw(wn[1] - 1) << setprecision(10) << i_prn[i].i_toch << char(179)
  95.             << setw(wn[2] - 1) << i_prn[i].i_sum << setprecision(6) << char(179)
  96.             << setw(wn[3] - 1) << i_prn[i].n << char(179) << endl;
  97.     }
  98.     //низ таблицы
  99.     cout << char(192) << setfill(char(196));
  100.     for (int j = 0; j < m - 1; j++)
  101.         cout << setw(wn[j]) << char(193);
  102.     cout << setw(wn[m - 1]) << char(217) << setfill(' ') << endl;
  103. }
  104.  
  105.  
  106. double IntRect(ftype f, double a, double b, double eps, int& n, int mode)
  107. {
  108.     n = 1;
  109.     double y = 0;
  110.     double Iold = 0, Inew = 0;
  111.     double left = a;
  112.     do {
  113.         n *= 2;
  114.         Iold = Inew;
  115.         double dx = (b - a) / n;
  116.         while (a < b) {
  117.             y += f(a + dx / 2, mode);
  118.             a += dx;
  119.         }
  120.         a = left;
  121.         Inew = y * dx;
  122.         y = 0;
  123.     } while (abs(Iold - Inew) > 3 * eps);
  124.     return Inew;
  125. }
  126.  
  127. double IntTrap(ftype f, double a, double b, double eps, int& n, int mode) {
  128.     n = 1;
  129.     double y = 0;
  130.     double Iold = 0, Inew = 0;
  131.     double left = a;
  132.     do {
  133.         n *= 2;
  134.         Iold = Inew;
  135.         double dx = (b - a) / n;
  136.         while (a < b) {
  137.             y += (f(a, mode) + f(a + dx, mode)) / 2;
  138.             a += dx;
  139.         }
  140.         a = left;
  141.         Inew = y * dx;
  142.         y = 0;
  143.     } while (abs(Inew - Iold) > 3 * eps);
  144.     return Inew;
  145. }
  146.  
  147. double funcs(double x, int mode) {
  148.     switch (mode) {
  149.     case 1: return x;
  150.     case 2: return sin(22 * x);
  151.     case 3: return x * x * x * x;
  152.     case 4: return atan(x);
  153.     }
  154. }
  155.  
  156. double i_toch(int mode) {
  157.     double a = -1, b = 3;
  158.     switch (mode) {
  159.     case 1: return (b * b - a * a) / 2.0;
  160.     case 2: return (cos(a * 22.0) - cos(b * 22.0)) / 22.0;
  161.     case 3: return (b * b * b * b * b - a * a * a * a * a) / 5.0;
  162.     case 4: return b * atan(b) - a * atan(a) - (log(b * b + 1) - log(a * a + 1)) / 2.0;
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement