Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <complex>
  4. #include<conio.h>
  5. #include <vector>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9. const int n = 5;
  10.  
  11. complex<double> k[n] = {
  12.         complex<double>(0, 1),
  13.         complex<double>(15, 0),
  14.         complex<double>(10, 0),
  15.         complex<double>(12, 0),
  16.         complex<double>(10, 0),
  17.  
  18. };
  19.  
  20. double
  21. coordDescent(double x01, double x02, const double E, const double alp01, const double alp02, complex<double> *koef,
  22.              int k);
  23.  
  24. complex<double> gorner1f(double x, double y, complex<double> koef[]);
  25.  
  26. double gornerf(double x, double y, complex<double> koef[]);
  27.  
  28. void gornerd(double x, double y, complex<double> *a);
  29.  
  30. //временные корни
  31. vector<double> x1t;
  32. vector<double> x2t;
  33. //нормальные корни
  34. vector<double> x1c;
  35. vector<double> x2c;
  36.  
  37. //счетчик функций
  38. vector<double> fc;
  39. int fco = 0;
  40.  
  41. int main() {
  42.     double eps = 0.01, Eps = 0.0000001;
  43.     complex<double> temp[n];
  44.     for (int i = 0; i < n; i++) {
  45.         temp[i] = k[i];
  46.     }
  47.     cout << "COORD DESCENT: " << endl;
  48.     cout << "Finding root 1 :" << endl;
  49.     coordDescent(0, 0, Eps, 1, 1, temp, 1);
  50.     gornerd(x1c[0], x2c[0], temp);
  51.     cout << endl;
  52.     cout << "fc: " << fco << endl;
  53.     fc.push_back(fco);
  54.     cout << endl;
  55.     cout << endl;
  56.     for (int i = 1; i < n - 2; i++) {
  57.         eps *= 2;
  58.         fco = 0;
  59.         cout << "Finding root " << i + 1 << ": " << endl;
  60.         coordDescent(0, 0, eps, 1, 1, temp, 2);
  61.         coordDescent(x1t[i - 1], x2t[i - 1], Eps, 1, 1, k, 1);
  62.         gornerd(x1c[i], x2c[i], temp);
  63.         cout << endl;
  64.         cout << "fc: " << fco << endl;
  65.         fc.push_back(fco);
  66.         cout << endl;
  67.         cout << endl;
  68.     }
  69.     fco = 0;
  70.     cout << "Finding root 4 :" << endl;
  71.     x1t.push_back((-temp[0] / temp[1]).real());
  72.     x2t.push_back((-temp[0] / temp[1]).imag());
  73.     coordDescent(x1t[2], x2t[2], Eps, 1, 1, k, 1);
  74.     cout << endl;
  75.     cout << "fc: " << fco << endl;
  76.     fc.push_back(fco);
  77.     cout << endl;
  78.     cout << endl;
  79.     cout << "fcall: " << fc[0] + fc[1] + fc[2] + fc[3] << endl;
  80.     return 0;
  81. }
  82.  
  83. // считает значение функции в точке
  84. double gornerf(double x, double y, complex<double> koef[]) {
  85.     return pow(gorner1f(x, y, koef).real(), 2) + pow(gorner1f(x, y, koef).imag(), 2);
  86. }
  87.  
  88. // считает значение исходного полинома в точке
  89. complex<double> gorner1f(double x, double y, complex<double> koef[]) {
  90.     complex<double> z(x, y);
  91.     complex<double> s = koef[n - 1];
  92.     for (int i = 1; i <= n - 1; ++i) {
  93.         s *= z;
  94.         s += koef[(n - 1) - i];
  95.     }
  96.     return s;
  97. }
  98.  
  99. // деление полинома на бином
  100. void gornerd(double x, double y, complex<double> *a) {
  101.     complex<double> c(x, y);
  102.     complex<double> b[n];
  103.     b[n - 1] = 0;
  104.     for (int i = n - 2; i >= 0; i--) {
  105.         b[i] = a[i + 1] + c * b[i + 1];
  106.     }
  107.     for (int j = 0; j < n; j++) {
  108.         a[j] = b[j];
  109.     }
  110. }
  111.  
  112. // координатный спуск
  113. double
  114. coordDescent(double x01, double x02, const double E, const double alp01, const double alp02, complex<double> *koef,
  115.              int k) {
  116.     double x1 = x01;
  117.     double x2 = x02;
  118.     double alp1 = alp01;
  119.     double alp2 = alp02;
  120.     int count2 = 0;
  121.     int count1 = 0;
  122.     int n = 2;
  123.     double xu = 0, xn = 1;
  124.  
  125.     double f1, f2, f;
  126.     f = (gornerf(x1, x2, koef));
  127.     fco++;
  128.     double eps = 0.00000001;
  129.     cout << "x1 " << x1 << " x2 " << x2 << " f " << f << " a1 " << alp1 << " a2 " << alp2 << endl;
  130.     while (abs(f) > E) {
  131.         alp1 = abs(alp1);
  132.         alp2 = abs(alp2);
  133.         f1 = gornerf(x1 + eps, x2, koef);
  134.         f2 = gornerf(x1 - eps, x2, koef);
  135.         fco = fco + 2;
  136.         cout << endl;
  137.         cout << setprecision(20) << "x1 " << x1 - eps << " x2 " << x2 << " f " << f2 << " a1 " << alp1 << " a2 " << alp2
  138.              << endl;
  139.         cout << setprecision(20) << "x1 " << x1 + eps << " x2 " << x2 << " f " << f1 << " a1 " << alp1 << " a2 " << alp2
  140.              << endl;
  141.         if (f1 <= f2) {
  142.             x1 = x1 + eps;
  143.             f = f1;
  144.             cout << "Xplus" << endl;
  145.         } else {
  146.             x1 = x1 - eps;
  147.             f = f2;
  148.             cout << "Xminus" << endl;
  149.             alp1 *= -1;
  150.         }
  151.         count1 = 0;
  152.         count2 = 0;
  153.         while (count1 < n && count2 < 1) {
  154.             f1 = gornerf(x1 + alp1, x2, koef);
  155.             fco++;
  156.             while (f1 < f) {
  157.                 x1 = x1 + alp1;
  158.                 cout << "OkX" << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1 << " a2 "
  159.                      << alp2 << endl;
  160.                 count2 += 1;
  161.                 f = f1;
  162.  
  163.                 if (count2 > 1)cout << "Second step with this ax" << endl;
  164.                 f1 = gornerf(x1 + alp1, x2, koef);
  165.  
  166.             }
  167.             if (f1 > f) {
  168.                 alp1 *= 0.5;
  169.                 cout << "NeokX " << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1
  170.                      << " a2 " << alp2 << endl;
  171.                 count1++;
  172.             }
  173.         }
  174.         count1 = 0;
  175.         count2 = 0;
  176.         f1 = gornerf(x1, x2 + eps, koef);
  177.         f2 = gornerf(x1, x2 - eps, koef);
  178.         fco = fco + 2;
  179.         cout << endl;
  180.         cout << setprecision(20) << "x1 " << x1 << " x2 " << x2 - eps << " f " << f2 << " a1 " << alp1 << " a2 " << alp2
  181.              << endl;
  182.         cout << setprecision(20) << "x1 " << x1 << " x2 " << x2 + eps << " f " << f1 << " a1 " << alp1 << " a2 " << alp2
  183.              << endl;
  184.  
  185.         if (f1 <= f2) {
  186.             x2 = x2 + eps;
  187.             f = f1;
  188.             cout << "Yplus" << endl;
  189.         } else {
  190.             x2 = x2 - eps;
  191.             f = f2;
  192.             cout << "Yminus" << endl;
  193.             alp2 *= -1;
  194.         }
  195.         while (count2 < n && count1 < 1) {
  196.             f1 = gornerf(x1, x2 + alp2, koef);
  197.             fco++;
  198.             while (f1 < f) {
  199.                 x2 = x2 + alp2;
  200.                 cout << "OkY " << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1 << " a2 "
  201.                      << alp2 << endl;
  202.                 count2 += 1;
  203.                 f = f1;
  204.                 if (count2 > 1)cout << "Second step with this ay" << endl;
  205.                 f1 = gornerf(x1, x2 + alp2, koef);
  206.  
  207.             }
  208.             if (f1 > f) {
  209.                 alp2 *= 0.5;
  210.                 cout << "NeokY " << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1
  211.                      << " a2 " << alp2 << endl;
  212.                 count2++;
  213.             }
  214.         }
  215.     }
  216.     if (k == 1) {
  217.         cout << endl;
  218.         cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  219.         x1c.push_back(x1);
  220.         x2c.push_back(x2);
  221.     }
  222.     if (k == 2) {
  223.         cout << endl;
  224.         cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  225.         x1t.push_back(x1);
  226.         x2t.push_back(x2);
  227.     }
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement