Advertisement
gashink_t

Untitled

May 28th, 2021
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <list>
  4. using namespace std;
  5. float eps = 0.0001;
  6.  
  7. float fun(float x, float y, float dy, float d2y);
  8. float* runge_kutt(float x, float* y, float h);
  9. float* double_recalculation(float a, float* y, float h);
  10. float mt_shoot(float* y, float h);
  11. float bisect(float x, float* y);
  12. float* get_interpol(float* x, float* y);
  13. float Aitken(float* X, float* y, float x);
  14. float Simpson(float a, float b, float n, float* interpol);
  15.  
  16. int main()
  17. {
  18.     float* interpol = new float[6];
  19.     float* x_n = new float[6];
  20.     float** answer = new float* [6];
  21.     float* Koshi = new float[2]{ 1, 6 };
  22.     float x = 0.0, h = 0.2, dy = mt_shoot(Koshi, h);
  23.     float* y = new float[2]{ Koshi[0], dy };
  24.     for (int i = 0; i < 6; i++) answer[i] = new float[3];
  25.  
  26.     for (int i = 0; i < 6; i++)
  27.         x_n[i] = i * h;
  28.  
  29.     int i = 0;
  30.     while (x <= 1)
  31.     {
  32.         answer[i][0] = x;
  33.         answer[i][1] = y[0];
  34.         answer[i][2] = y[1];
  35.         interpol[i] = y[0];
  36.         i++; x += h;
  37.         y = runge_kutt(x, y, h);
  38.     }
  39.     answer[i][0] = x; answer[i][1] = Koshi[1]; answer[i][2] = y[1];
  40.     interpol[i] = Koshi[1];
  41.     float* interpol_res = get_interpol(x_n, interpol);
  42.     cout << "Integral: " << Simpson(0, 1, 1000, interpol_res) << endl;
  43.     for (i = 0; i < 6; i++)
  44.     {
  45.         for (int j = 0; j < 2; j++)
  46.             cout << answer[i][j];
  47.         cout << answer[i][2];
  48.         cout << endl;
  49.     }
  50.  
  51.     return 0;
  52. }
  53.  
  54. float fun(float x, float y, float dy, float d2y)
  55. {
  56.     return pow(d2y, 5) - 7 * cos(x) * d2y - 10 * x - 4 * exp(x) * dy + (5 * y) / (x + 6);
  57. }
  58.  
  59. float f(float x, float* y)
  60. {
  61.     float z = 0; int i;
  62.     for (i = 0; z < x; i++) z += 0.01;
  63.  
  64.     return y[i];
  65. }
  66.  
  67. float bisect(float x, float* y)
  68. {
  69.     float a = 0, b = 1, fun_a = 1, fun_b = 1, fun_differ, differ;
  70.  
  71.     while (fun_a * fun_b > 0)
  72.     {
  73.         a--; b++;
  74.         fun_a = fun(x, y[0], y[1], a);
  75.         fun_b = fun(x, y[0], y[1], b);
  76.     }
  77.  
  78.     while (true)
  79.     {
  80.         fun_a = fun(x, y[0], y[1], a);
  81.         differ = (a + b) / 2;
  82.         fun_differ = fun(x, y[0], y[1], differ);
  83.  
  84.         if (fabs(fun_differ) < eps)
  85.             break;
  86.  
  87.         if (fun_a * fun_differ < 0)
  88.             b = differ;
  89.         else a = differ;
  90.  
  91.     }
  92.  
  93.     return differ;
  94. }
  95.  
  96. float* get_interpol(float* x, float* y)
  97. {
  98.     float* res = new float[102];
  99.     int k = 0;
  100.     for (float i = 0; i <= 1.01; i += 0.01)
  101.     {
  102.         res[k] = (Aitken(x, y, i));
  103.         k++;
  104.     }
  105.  
  106.     return res;
  107. }
  108.  
  109. float Aitken(float* X, float* y, float x)
  110. {
  111.     float* P = new float[sizeof(y)];
  112.     for (int i = 0; i < sizeof(y); i++) P[i] = y[i];
  113.  
  114.     for (int j = 1; j < sizeof(y); j++)
  115.         for (int i = 0; i < sizeof(y) - j; i++)
  116.             P[i] = (P[i] * (X[i + j] - x) - P[i + 1] * (X[i] - x)) / (X[i + j] - X[i]);
  117.  
  118.     return P[0];
  119. }
  120.  
  121. float Simpson(float a, float b, float n, float* interpol)
  122. {
  123.     int k;
  124.     float h = (b - a) / n;
  125.     float answer = f(a, interpol) + f(b, interpol);
  126.  
  127.     for (int i = 1; i < n; i++)
  128.     {
  129.         if (i % 2 == 0) k = 2;
  130.         else k = 4;
  131.         answer += k * f(a + i * h, interpol);
  132.     }
  133.     answer *= h / 3;
  134.  
  135.     return answer;
  136. }
  137.  
  138. float* runge_kutt(float x, float* y, float h)
  139. {
  140.     float* y_current = new float[2];
  141.     y_current[0] = y[0]; y_current[1] = y[1];
  142.  
  143.     for (int i = 0; i < 2; i++)
  144.     {
  145.         y_current[0] = y_current[0] + (h / 2) * y_current[1];
  146.         y_current[1] = y_current[1] + (h / 2) * bisect(x + h, y_current);
  147.     }
  148.  
  149.     return y_current;
  150. }
  151.  
  152. float* double_recalculation(float a, float* y, float h)
  153. {
  154.     float* y_1 = new float[2];
  155.     float* y_2 = new float[2];
  156.     float x_i = 0;
  157.     for (int i = 0; i < 2; i++)
  158.     {
  159.         y_1[0] = y_2[0] = 1;
  160.         y_1[1] = y_2[1] = a;
  161.     }
  162.  
  163.     while (x_i <= 1)
  164.     {
  165.         y_1 = runge_kutt(x_i, y_1, h);
  166.         y_2 = runge_kutt(x_i, y_2, h / 2);
  167.         y_2 = runge_kutt(x_i + h / 2, y_2, h / 2);
  168.  
  169.         if (2 * eps <= fabs(y_1[0] - y_2[0]))
  170.         {
  171.             x_i = 0;
  172.             h /= 2;
  173.             y_1 = new float[2]{ 1, a };
  174.             y_2 = new float[2]{ 1, a };
  175.         }
  176.         else x_i += h;
  177.     }
  178.  
  179.     y[0] = y_2[0]; y[1] = y_2[1];
  180.  
  181.     return y;
  182. }
  183.  
  184. float mt_shoot(float* y, float h)
  185. {
  186.     float a = 0, b = 1;
  187.     float* y_a = new float[2];
  188.     float* y_b = new float[2];
  189.     float* y_c = new float[2];
  190.     y_a = double_recalculation(a, y_a, h);
  191.  
  192.     bool f = y_a[1] < y[1] ? true : false;
  193.     while (f == (y_a[1] < y[1]))
  194.     {
  195.         if (y_a[1] < y[1])
  196.         {
  197.             f = true;
  198.             a = b;
  199.             b += h;
  200.         }
  201.         else
  202.         {
  203.             f = false;
  204.             b = a;
  205.             a -= h;
  206.         }
  207.         y_a = double_recalculation(a, y_a, h);
  208.     }
  209.  
  210.     b = a; a -= h;
  211.     y_a = double_recalculation(a, y_a, h);
  212.     y_b = double_recalculation(b, y_b, h);
  213.  
  214.     float c = 0;
  215.     while (fabs(y[1] - y_c[1]) > 0.2)
  216.     {
  217.         c = (a + b) / 2;
  218.         y_c = double_recalculation(c, y_c, h);
  219.         if ((y_a[1] - y[1]) * (y_b[1] - y[1]) < 0)
  220.         {
  221.             b = c;
  222.             y_b = y_c;
  223.         }
  224.         else
  225.         {
  226.             a = c;
  227.             y_a = y_c;
  228.         }
  229.     }
  230.  
  231.     return c;
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement