Advertisement
baadgeorge

2

Nov 11th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 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. void GradDescentStep(double current_x1, double current_x2, double current_x3);
  15.  
  16. int main()
  17. {
  18.     setlocale(LC_ALL, "RUSSIAN");
  19.  
  20.     double x1 = 1;
  21.     double x2 = 2;
  22.     double x3 = 3;
  23.  
  24.     //Метод градиентного спуска с дроблением шага
  25.     GradDescentStep(x1, x2, x3);
  26.     cout << endl;
  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. //Метод градиентного спуска с дроблением шага
  59. void GradDescentStep(double current_x1, double current_x2, double current_x3)
  60. {
  61.     //начальная установка
  62.     double next_x1 = current_x1;
  63.     double next_x2 = current_x2;
  64.     double next_x3 = current_x3;
  65.     double step = 1;        //шаг
  66.     int iterations = 0;     //количество итераций
  67.     double eps = 0.0001;     //точность
  68.  
  69.     double f = 0;
  70.     double f_delta = 0;
  71.  
  72.     cout << " >> Градиентный спуск с дроблением шага:\n";
  73.     cout << setw(11) << left << " Итерация" << setw(12) << left << "x1 " << setw(12) << left << "x2 " << setw(12) << left << "x3 "
  74.         << setw(16) << left << "||gradf(x^k)|| " << setw(12) << left << " f(x^k)\n" << endl;
  75.  
  76.     while (grad(next_x1, next_x2, next_x3) > eps)
  77.     {
  78.         current_x1 = next_x1;   //определение переменных текущей итерации
  79.         current_x2 = next_x2;
  80.         current_x3 = next_x3;
  81.  
  82.         //получение значений функции и функции с учетом градиента
  83.         f = function(next_x1, next_x2, next_x3);
  84.         f_delta = function(next_x1 - step * grad_x1(current_x1, current_x2, current_x3), next_x2 - step * grad_x2(current_x1, current_x2, current_x3), next_x3 - step * grad_x3(current_x1, current_x2, current_x3));
  85.  
  86.         if (f > f_delta)
  87.         {
  88.             next_x1 = current_x1 - step * grad_x1(current_x1, current_x2, current_x3);
  89.             next_x2 = current_x2 - step * grad_x2(current_x1, current_x2, current_x3);
  90.             next_x3 = current_x3 - step * grad_x3(current_x1, current_x2, current_x3);
  91.         }
  92.         else
  93.             step = step * 0.5; //уменьшение шага
  94.  
  95.         iterations++;
  96.         cout << " " << setw(10) << left << iterations << setw(12) << left << next_x1 << setw(12) << left << next_x2 << setw(12) << left << next_x3
  97.             << setw(16) << left << grad(next_x1, next_x2, next_x3) << setw(12) << left << function(next_x1, next_x2, next_x3) << endl;
  98.     }
  99.     cout << "\nПолученная точка минимума: (" << next_x1 << ", " << next_x2 << ", " << next_x3 << ")" << endl;
  100.     cout << "Количество итераций: " << iterations << endl;
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement