gashink_t

method_Nyton_for_system( lab_5,vm)

Mar 6th, 2021 (edited)
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <iomanip>
  5. using namespace std;
  6. #define N 2
  7.  
  8. float f1(float x, float y);
  9. float f2(float x, float y);
  10. float f1_dx(float x, float y);
  11. float f2_dx(float x, float y);
  12. float f1_dy(float x, float y);
  13. float f2_dy(float x, float y);
  14. void matr_yakobi(float** SYSTEM);
  15. void Nyton_for_system(float x, float y, float eps);
  16.  
  17. int main()
  18. {
  19.     float x0, y0, E;
  20.     cout << "Enter the X0 = "; cin >> x0;
  21.     cout << "Enter the Y0 = "; cin >> y0;
  22.     cout << "Enter the Epsilon = "; cin >> E;
  23.     Nyton_for_system(x0, y0, E);
  24.  
  25.     return 0;
  26. }
  27.  
  28. void matr_yakobi(float** SYSTEM)
  29. {
  30.     float det, temp;
  31.     det = SYSTEM[0][0] * SYSTEM[1][1] - SYSTEM[0][1] * SYSTEM[1][0];
  32.     temp = SYSTEM[0][0];
  33.     SYSTEM[0][0] = SYSTEM[1][1] / det;
  34.     SYSTEM[1][1] = temp / det;
  35.     temp = SYSTEM[0][1];
  36.     SYSTEM[0][1] = -SYSTEM[1][0] / det;
  37.     SYSTEM[1][0] = -temp / det;
  38. }
  39.  
  40. void Nyton_for_system(float x, float y, float e)
  41. {
  42.     int k = 1;
  43.     float x_temp;
  44.     float** system = new float*[N];
  45.     for (int i = 0; i < N; i++)
  46.     {
  47.         system[i] = new float[N];
  48.     }
  49.  
  50.     while (sqrt(pow(f1(x, y), 2) + pow(f2(x, y), 2)) >= e)
  51.     {
  52.         system[0][0] = f1_dx(x, y);
  53.         system[0][1] = f2_dx(x, y);
  54.         system[1][0] = f1_dy(x, y);
  55.         system[1][1] = f2_dy(x, y);
  56.         matr_yakobi(system);
  57.         x_temp = x;
  58.         x += -(system[0][0] * f1(x, y) + system[0][1] * f2(x, y));
  59.         y += -(system[1][0] * f1(x_temp, y) + system[1][1] * f2(x_temp, y));
  60.         k++;
  61.         cout << "Iteration " << k << ":\tX = " << setw(12) << round(x*100)/100 << "\tY = " << setw(12) << round(y*100)/100 << endl;
  62.     }
  63.  
  64.     cout << "Answer:\nX = " << round(x*1000)/1000 << "\nY = " << round(y*1000)/1000 << endl;
  65. }
  66.  
  67. float f1(float x, float y)
  68. {
  69.     return sin(2 * x - y) - 1.2 * x - 0.4;
  70. }
  71.  
  72. float f2(float x, float y)
  73. {
  74.     return 0.8 * pow(x, 2) + 1.5 * pow(y, 2) - 1;
  75. }
  76.  
  77. float f1_dx(float x, float y)
  78. {
  79.     return 2 * cos(2 * x - y) - 1.2;
  80. }
  81.  
  82. float f2_dx(float x, float y)
  83. {
  84.     return 1.6 * x;
  85. }
  86.  
  87. float f1_dy(float x, float y)
  88. {
  89.     return -cos(2 * x - y);
  90. }
  91.  
  92. float f2_dy(float x, float y)
  93. {
  94.     return 3 * y;
  95. }
Add Comment
Please, Sign In to add comment