Advertisement
CopyPasterBl

Вычмат ЛР 1

Mar 19th, 2023
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. float a = 2;
  6. float b = -1.3f;
  7. float c = 0.04f;
  8. float d = 0.12f;
  9. float eps = 10e-4;
  10. float x = 0;
  11. float y = 0;
  12. float Alpha = 1.0f;
  13.  
  14. float f(float InX, float InY)
  15. {
  16.     return a * x + b * y + exp(c * pow(x, 2) + d * pow(y, 2));
  17. }
  18.  
  19. float DerivativeX(float InX, float InY)
  20. {
  21.     return 2 * c * x * exp(c * pow(x, 2) + d * pow(y, 2)) + a;
  22. }
  23.  
  24. float DerivativeY(float InX, float InY)
  25. {
  26.     return 2 * d * y * exp(c * pow(x, 2) + d * pow(y, 2)) + b;
  27. }
  28.  
  29. int main()
  30. {
  31.     float NextX = NULL;
  32.     float NextY = NULL;
  33.  
  34.     cout << "x" << "\t" << "y" << "\t" << "f" << "\t" << "f'x" << "\t" << "f'y" << "\t" << "Alpha" << endl;
  35.    
  36.     while (sqrt(pow(DerivativeX(x, y), 2) + pow(DerivativeY(x, y), 2)) >= eps)
  37.     {
  38.         cout << x << " " << y << " " << f(x, y) << " " << DerivativeX(x, y) << " " << DerivativeY(x, y) << " " << Alpha << endl;
  39.  
  40.         NextX = x - Alpha * DerivativeX(x, y);
  41.         NextY = y - Alpha * DerivativeY(x, y);
  42.  
  43.         if (f(x, y) > f(x, y))
  44.         {
  45.             Alpha /= 2;
  46.         }
  47.         else
  48.         {
  49.             x = NextX;
  50.             y = NextY;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement