Advertisement
baadgeorge

3

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