Advertisement
Vladislav_Bezruk

Untitled

Jun 7th, 2022
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x)
  7. {
  8.     return -3 * x / (pow(x, 2) + 1);
  9. }
  10.  
  11. int min(double* x)
  12. {
  13.     if (f(x[0]) < min(f(x[1]), f(x[2])))
  14.     {
  15.         return 0;
  16.     }
  17.    
  18.     if (f(x[1]) < min(f(x[0]), f(x[2])))
  19.     {
  20.         return 1;
  21.     }
  22.    
  23.     return 2;
  24. }
  25.  
  26. int max(double* x, double x_ser)
  27. {
  28.     if (f(x[0]) > max(f(x[1]), max(f(x[2]), f(x_ser))))
  29.     {
  30.         return 0;
  31.     }
  32.    
  33.     if (f(x[1]) > max(f(x[0]), max(f(x[2]), f(x_ser))))
  34.     {
  35.         return 1;
  36.     }
  37.    
  38.     if (f(x[2]) > max(f(x[1]), max(f(x[0]), f(x_ser))))
  39.     {
  40.         return 2;
  41.     }
  42.    
  43.     return 3;
  44. }
  45.  
  46. double* sort_x(double* x)
  47. {
  48.     for (int i = 0; i < 2; i++)
  49.     {
  50.         if (x[0] > x[1])
  51.         {
  52.             swap(x[0], x[1]);
  53.         }
  54.    
  55.         if (x[1] > x[2])
  56.         {
  57.             swap(x[1], x[2]);
  58.         }
  59.     }
  60.    
  61.     return x;
  62. }
  63.  
  64. double* calc_x(double* x, double x_ser)
  65. {
  66.     int i_max = max(x, x_ser);
  67.    
  68.     if (i_max != 3)
  69.     {
  70.         x[i_max] = x_ser;
  71.     }
  72.    
  73.     return sort_x(x);
  74. }
  75.  
  76. double calc(double h, double a, double b, double eps_x, double eps_y)
  77. {
  78.     int i = 0;
  79.     double* x = new double[3], x_ser, x_prec = 500, y_prec = 500, x_min, f_min, x_prec_p, y_prec_p ;
  80.    
  81.     x[0] = (b - a) / 4;
  82.     x[1] = x[0] + h;
  83.    
  84.     if (f(x[1]) > f(x[0]))
  85.     {
  86.         x[2] = x[0] - h;
  87.     }
  88.     else
  89.     {
  90.         x[2] = x[0] + 2 * h;
  91.     }
  92.    
  93.     while (true)
  94.     {
  95.         x_ser = 0.5 * ((f(x[0]) * (pow(x[1], 2) - pow(x[2], 2)) + f(x[1]) * (pow(x[2], 2) - pow(x[0], 2)) + f(x[2]) * (pow(x[0], 2) - pow(x[1], 2))) / ((f(x[0]) * (x[1] - x[2]) + f(x[1]) * (x[2] - x[0]) + f(x[2]) * (x[0] - x[1]))));
  96.        
  97.         x_min = x[min(x)];
  98.         f_min = f(x_min);
  99.        
  100.         x_prec_p = x_prec;
  101.         y_prec_p = y_prec;
  102.        
  103.         x_prec = fabs((x_min - x_ser) / x_ser);
  104.         y_prec = fabs((f_min - f(x_ser)) / f(x_ser));
  105.        
  106.         cout << "x1 = " << x[0] << " x2 = " << x[1] << " x3 = " << x[2] << " h = " << h << endl;
  107.         cout << "x_prec = " << x_prec << " y_prec = " << y_prec << endl << endl;
  108.                
  109.         if (eps_x > x_prec && eps_y > y_prec)
  110.         {
  111.             break;
  112.         }
  113.        
  114.         if (fabs(x_prec - x_prec_p) < 1e-2 && fabs(y_prec - y_prec_p) < 1e-2)
  115.         {
  116.             return calc(h / 2, a, b, eps_x, eps_y);
  117.         }
  118.        
  119.         i++;
  120.        
  121.         if (i > 10) {
  122.             return 0;
  123.         }
  124.        
  125.         x = calc_x(x, x_ser);
  126.     }
  127.    
  128.     return f_min > f(x_ser) ? x_ser : x_min;
  129. }
  130.  
  131. int main()
  132. {
  133.     double xsr, a, b, eps_x, eps_y, h, x_min;
  134.    
  135.     h = 1;
  136.     a = 0;
  137.     b = 5;
  138.     eps_x = 0.03;
  139.     eps_y = 0.003;
  140.    
  141.     x_min = calc(h, a, b, eps_x, eps_y);
  142.    
  143.     cout << "x_min = " << x_min << endl;
  144.     cout << "f_min = " << f(x_min) << endl;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement