Advertisement
baadgeorge

4

Nov 11th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. double function(double x1, double x2, double x3); // исходная функция
  7. double grad_x1(double x1, double x2, double x3);  // чп по х1
  8. double grad_x2(double x1, double x2, double x3);  // чп по х2
  9. double grad_x3(double x1, double x2, double x3);  // чп по х3
  10. double grad(double x1, double x2, double x3);     // градиент функции
  11.  
  12. double StepCalculator(double x1, double x2, double x3, double p_x1, double p_x2, double p_x3);
  13. //Метод покоординатного спуска
  14. void CoordinateDescent(double current_x1, double current_x2, double current_x3);
  15. int main()
  16. {
  17.     setlocale(LC_ALL, "RUSSIAN");
  18.     double x1 = 1;
  19.     double x2 = 2;
  20.     double x3 = 3;
  21.     //Метод покоординатного спуска
  22.     CoordinateDescent(x1, x2, x3);
  23.     system("pause");
  24.     return 0;
  25. }
  26. double function(double x1, double x2, double x3)
  27. {
  28.     return 3 * pow(x1, 2) + 4 * pow(x2, 2) + 6 * pow(x3, 2) + 2 * x1 * x2 + 2 * x1 - 3 * x2 + 5 * x3;
  29. }
  30. double grad_x1(double x1, double x2, double x3)
  31. {
  32.     return 6 * x1 + 2 * x2 + 2;
  33. }
  34. double grad_x2(double x1, double x2, double x3)
  35. {
  36.     return 8 * x2 + 2 * x1 - 3;
  37. }
  38. double grad_x3(double x1, double x2, double x3)
  39. {
  40.     return 12 * x3 + 5;
  41. }
  42.  
  43. double grad(double x1, double x2, double x3)
  44. {
  45.     double gr_x1 = grad_x1(x1, x2, x3);
  46.     double gr_x2 = grad_x2(x1, x2, x3);
  47.     double gr_x3 = grad_x3(x1, x2, x3);
  48.  
  49.     return sqrt(pow(gr_x1, 2) + pow(gr_x2, 2) + pow(gr_x3, 2));
  50. }
  51.  
  52. //Подсчет значения шага
  53. double StepCalculator(double x1, double x2, double x3, double p_x1, double p_x2, double p_x3)
  54. {
  55.     double step = 0;
  56.     double numerator = 0;
  57.     double denominator = 0;
  58.     //подсчет выражения для определения шага
  59.     numerator = 6 * x1 * p_x1 + 8 * x2 * p_x2 + 12 * x3 * p_x3 + 2 * x1 * p_x2 + 2 * x2 * p_x1 + 2 * p_x1 - 3 * p_x2 + 5 * p_x3;
  60.     denominator = 6 * pow(p_x1, 2) + 8 * pow(p_x2, 2) + 12 * pow(p_x3, 2) + 4 * p_x1 * p_x2;
  61.     step = -numerator / denominator;
  62.     return step;
  63. }
  64.  
  65. //Метод покоординатного спуска
  66. void CoordinateDescent(double current_x1, double current_x2, double current_x3)
  67. {
  68.     //начальная установкаcurrent
  69.     double next_x1 = current_x1;
  70.     double next_x2 = current_x2;
  71.     double next_x3 = current_x3;
  72.     double step = 0; //шаг
  73.     int iterations = 0; //количество итераций
  74.     double eps = 0.0001; //точность
  75.     //элементы вектора P
  76.     double p_x1 = 0;
  77.     double p_x2 = 0;
  78.     double p_x3 = 0;
  79.     cout << " >> Покоординатный спуск:\n";
  80.     cout << setw(11) << left << " Итерация" << setw(12) << left << "x1 " << setw(12) << left << "x2 " << setw(12) << left << "x3 "
  81.         << setw(18) << left << "||grad f(x1^k)|| " << setw(12) << left << " f(x1^k)\n" << endl;
  82.     while (grad(next_x1, next_x2, next_x3) > eps)
  83.     {
  84.         //определение переменных текущей итерации
  85.         current_x1 = next_x1;
  86.         current_x2 = next_x2;
  87.         current_x3 = next_x3;
  88.         //подсчет значений элементов вектора P
  89.         p_x1 = -grad_x1(current_x1, current_x2, current_x3);
  90.         p_x2 = -grad_x2(current_x1, current_x2, current_x3);
  91.         p_x3 = -grad_x3(current_x1, current_x2, current_x3);
  92.  
  93.         step = StepCalculator(current_x1, current_x2, current_x3, p_x1, p_x2, p_x3); // получение значения шага для x1
  94.         next_x1 = current_x1 + step * p_x1; //расчет следующего значения переменной х
  95.         //подсчет значений элементов вектора L
  96.         p_x1 = -grad_x1(next_x1, current_x2, current_x3);
  97.         p_x2 = -grad_x2(next_x1, current_x2, current_x3);
  98.         p_x3 = -grad_x3(next_x1, current_x2, current_x3);
  99.  
  100.         step = StepCalculator(next_x1, current_x2, current_x3, p_x1, p_x2, p_x3);
  101.         next_x2 = current_x2 + step * p_x2;
  102.         p_x1 = -grad_x1(next_x1, next_x2, current_x3);
  103.         p_x2 = -grad_x2(next_x1, next_x2, current_x3);
  104.         p_x3 = -grad_x3(next_x1, next_x2, current_x3);
  105.  
  106.         step = StepCalculator(next_x1, next_x2, current_x3, p_x1, p_x2, p_x3);
  107.         next_x3 = current_x3 + step * p_x3;
  108.         iterations++;
  109.         cout << " " << setw(10) << left << iterations << setw(12) << left << next_x1 << setw(12) << left << next_x2 << setw(12) << left << next_x3
  110.             << setw(16) << left << grad(next_x1, next_x2, next_x3) << setw(12) << left << function(next_x1, next_x2, next_x3) << endl;
  111.     }
  112.     cout << "\nПолученная точка минимума: (" << next_x1 << ", " << next_x2 << ", " << next_x3 << ")" << endl;
  113.     cout << "Количество итераций: " << iterations << endl;
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement