Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.41 KB | None | 0 0
  1. // Nikirova Yuliya Group 205
  2. #include <iostream>
  3. #include <cmath>
  4. #include <complex>
  5. #include<conio.h>
  6. #include <vector>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10. const int n = 5;
  11.  
  12. complex<double> k[n] = {
  13.         complex<double>(1, 1),
  14.         complex<double>(15, 0),
  15.         complex<double>(10, 0),
  16.         complex<double>(12, 0),
  17.         complex<double>(10, 0),
  18.  
  19. };
  20.  
  21. double
  22. coordDescent(double x01, double x02, const double E, const double alp01, const double alp02, complex<double> *koef,
  23.              int k);
  24.  
  25. double gornerf(double x, double y, complex<double> koef[]);
  26.  
  27. void gornerd(double x, double y, complex<double> *a);
  28.  
  29. vector<double> gradient(double x, double y, complex<double> *koef);
  30.  
  31. double ConstGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k);
  32.  
  33. double InAdvGrad(double x01, double x02, const double E, complex<double> *koef, int m);
  34.  
  35. double DivGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k);
  36.  
  37. double goldsec(double E, double a0, double b0, double x1, double x2, double alpha, complex<double> *koef);
  38.  
  39. double funcForOneDimMinimize(double x1, double x2, double alpha, complex<double> *koef);
  40.  
  41. double AccelGrad(double x01, double x02, const double E, complex<double> *koef, int k);
  42.  
  43. complex<double> gorner1f(double x, double y, complex<double> koef[]);
  44.  
  45. //Корни для итоговой таблицы для каждого метода
  46. vector<double> xk1;
  47. vector<double> yk1;
  48. vector<double> fk1;
  49. vector<double> xk2;
  50. vector<double> yk2;
  51. vector<double> fk2;
  52. vector<double> xk3;
  53. vector<double> yk3;
  54. vector<double> fk3;
  55. vector<double> xk4;
  56. vector<double> yk4;
  57. vector<double> fk4;
  58. vector<double> xk5;
  59. vector<double> yk5;
  60. vector<double> fk5;
  61.  
  62. // Вектор точных корней
  63. vector<double> x1n;
  64. vector<double> x2n;
  65. vector<double> fn;
  66.  
  67. // Вектор приблизительных корней
  68. vector<double> x1s;
  69. vector<double> x2s;
  70. vector<double> fs;
  71.  
  72. // Вектор подсчетов иттераций, вызовов функции, вызовов градиента пометодно
  73. vector<double> ittv;
  74. vector<double> fcv;
  75. vector<double> gcv;
  76.  
  77. // Переменная подсчетов иттераций, вызовов функции, вызовов градиента
  78. int itt = 1;
  79. int fc = 1;
  80. int gc = 1;
  81.  
  82. int ff1=0;
  83. int ff2=0;
  84. int ff=0;
  85.  
  86. int main() {
  87.     const double E = 0.0001; //для точных поисков
  88.     double eps = 0.0001; //для неточных поисков
  89.     double x1 = 0;
  90.     double x2 = 0;
  91.     complex<double> temp[n];
  92.     for (int i = 0; i < n; i++) {
  93.         temp[i] = k[i];
  94.     }
  95.  
  96.     //Покоординатный спуск
  97.     cout << "COORD DESCENT:" << endl;
  98.     cout << "Finding root " << 1 << endl;
  99.     coordDescent(x1, x2, E, 1, 1, temp, 1);
  100.     cout << endl;
  101.     cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
  102.          << fn[0] << endl;
  103.     ff = ff1+ff2;
  104.     cout<<"f: "<<ff1+ff2<<endl;
  105.     ff1 = 0;
  106.     ff2 = 0;
  107.     cout << endl;
  108.     xk1.push_back(x1n[0]);
  109.     yk1.push_back(x2n[0]);
  110.     fk1.push_back(fn[0]);
  111.     gornerd(x1n[0], x2n[0], temp);
  112.     for (int i = 1; i < n - 2; i++) {
  113.         cout << "Finding root " << i + 1 << endl;
  114.         coordDescent(x1, x2, eps, 1, 1, temp, 2);
  115.         eps *= 2;
  116.         coordDescent(x1s[i - 1], x2s[i - 1], E, 0.002, 0.002, k, 1);
  117.         cout << endl;
  118.         cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
  119.              << fn[i] << endl;
  120.         cout<<"f: "<<ff1+ff2<<endl;
  121.         ff = ff+ff1+ff2;
  122.         ff1 = 0;
  123.         ff2 = 0;
  124.         cout << endl;
  125.  
  126.         gornerd(x1n[i], x2n[i], temp);
  127.         xk1.push_back(x1n[i]);
  128.         yk1.push_back(x2n[i]);
  129.         fk1.push_back(fn[i]);
  130.     }
  131.     x1s.push_back((-temp[0] / temp[1]).real());
  132.     x2s.push_back((-temp[0] / temp[1]).imag());
  133.     fs.push_back(0);
  134.     cout << "Finding root " << n-1 << endl;
  135.     cout << "Aprox root:  " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
  136.          << x2s[n - 3] << " " << "f:"
  137.          << fs[n - 3] << endl;
  138.     coordDescent(x1s[n - 3], x2s[n - 3], E/10, 0.002, 0.002, k, 1);
  139.     cout << endl;
  140.     cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
  141.          << fn[n-2] << endl;
  142.     cout<<"f: "<<ff1+ff2<<endl;
  143.     ff = ff+ff1+ff2;
  144.     ff1 = 0;
  145.     ff2 = 0;
  146.     xk1.push_back(x1n[n - 2]);
  147.     yk1.push_back(x2n[n - 2]);
  148.     fk1.push_back(fn[n - 2]);
  149.     cout << endl;
  150.     //cout << "Count of itterations: " << itt << endl;
  151.     cout << "Count of fcalc: " << ff << endl;
  152.     cout << endl;
  153.  
  154.     // Обнуляем показатели для следующего метода
  155.     for (int i = 0; i < n; i++) {
  156.         temp[i] = k[i];
  157.     }
  158.     ittv.push_back(itt);
  159.     fcv.push_back(ff);
  160.     ff = 0;
  161.     ff1 = 0;
  162.     ff2 = 0;
  163.     gcv.push_back(0);
  164.     x1n.clear();
  165.     x2n.clear();
  166.     fn.clear();
  167.     itt = 1;
  168.     fc = 1;
  169.     gc = 1;
  170.     eps = 0.01;
  171.  
  172.     // Градиентный метод с постоянным шагом
  173.     cout << "CONST GRAD METHOD:" << endl;
  174.     cout << "Finding root " << 1 << endl;
  175.     ConstGrad(0, 0, E, 0.000428, temp, 1);
  176.     cout << endl;
  177.     cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
  178.          << fn[0] << endl;
  179.     ff = ff1+ff2;
  180.     cout<<"f: "<<ff1+ff2<<endl;
  181.     ff1 = 0;
  182.     ff2 = 0;
  183.     gornerd(x1n[0], x2n[0], temp);
  184.     xk2.push_back(x1n[0]);
  185.     yk2.push_back(x2n[0]);
  186.     fk2.push_back(fn[0]);
  187.     for (int i = 1; i < n - 2; i++) {
  188.         cout << endl;
  189.         cout << "Finding root " << i + 1 << endl;
  190.         ConstGrad(0, 0, eps, 0.000428, temp, 2);
  191.         eps *= 2;
  192.         ConstGrad(x1s[i - 1], x2s[i - 1], E, 0.00009, k, 1);
  193.         cout << endl;
  194.         cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
  195.              << fn[i] << endl;
  196.         ff =ff+ ff1+ff2;
  197.         cout<<"f: "<<ff1+ff2<<endl;
  198.         ff1 = 0;
  199.         ff2 = 0;
  200.         gornerd(x1n[i], x2n[i], temp);
  201.         xk2.push_back(x1n[i]);
  202.         yk2.push_back(x2n[i]);
  203.         fk2.push_back(fn[i]);
  204.     }
  205.     x1s.push_back((-temp[0] / temp[1]).real());
  206.     x2s.push_back((-temp[0] / temp[1]).imag());
  207.     fs.push_back(0);
  208.     cout << "Finding root " << n-1 << endl;
  209.     cout << "Aprox root:  "  << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
  210.          << x2s[n - 3] << " " << "f:"
  211.          << fs[n - 3] << endl;
  212.     eps *= 2;
  213.     ConstGrad(x1s[n-3], x2s[n-3], E/10, 0.00009, k, 1);
  214.     cout << endl;
  215.     cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
  216.          << fn[n-2] << endl;
  217.     ff =ff+ ff1+ff2;
  218.     cout<<"f: "<<ff1+ff2<<endl;
  219.     ff1 = 0;
  220.     ff2 = 0;
  221.     cout << endl;
  222.     xk2.push_back(x1n[n - 2]);
  223.     yk2.push_back(x2n[n - 2]);
  224.     fk2.push_back(fn[n - 2]);
  225.     //cout << "Count of itterations: " << itt << endl;
  226.     cout << "Count of fcalc: " << ff << endl;
  227.    // cout << "Count of gradcalc: " << gc << endl;
  228.     cout << endl;
  229.  
  230.     // Обнуляем показатели для следующего метода
  231.     for (int i = 0; i < n; i++) {
  232.         temp[i] = k[i];
  233.     }
  234.     ittv.push_back(itt);
  235.     fcv.push_back(ff);
  236.     gcv.push_back(gc);
  237.     x1n.clear();
  238.     x2n.clear();
  239.     fn.clear();
  240.     itt = 1;
  241.     fc = 1;
  242.     gc = 1;
  243.     eps = 0.01;
  244.     ff =0;
  245.     ff1 = 0;
  246.     ff2 = 0;
  247.  
  248.  
  249.     cout << "IN ADVANCE GRAD:" << endl;
  250.     cout << "Finding root " << 1 << endl;
  251.     InAdvGrad(x1, x2, E, temp, 1);
  252.     cout << endl;
  253.     cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
  254.          << fn[0] << endl;
  255.     ff =ff+ ff1+ff2;
  256.     cout<<"f: "<<ff1+ff2<<endl;
  257.     ff1 = 0;
  258.     ff2 = 0;
  259.     cout << endl;
  260.     xk3.push_back(x1n[0]);
  261.     yk3.push_back(x2n[0]);
  262.     fk3.push_back(fn[0]);
  263.     gornerd(x1n[0], x2n[0], temp);
  264.     for (int i = 1; i < n - 2; i++) {
  265.         cout << endl;
  266.         cout << "Finding root " << i + 1 << endl;
  267.         InAdvGrad(x1, x2, eps, temp, 2);
  268.         eps *= 2;
  269.         InAdvGrad(x1s[i-1], x2s[i-1], E, k, 1);
  270.         cout << endl;
  271.         cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
  272.              << fn[i] << endl;
  273.         ff =ff+ ff1+ff2;
  274.         cout<<"f: "<<ff1+ff2<<endl;
  275.         ff1 = 0;
  276.         ff2 = 0;
  277.         gornerd(x1n[i], x2n[i], temp);
  278.         xk3.push_back(x1n[i]);
  279.         yk3.push_back(x2n[i]);
  280.         fk3.push_back(fn[i]);
  281.     }
  282.     x1s.push_back((-temp[0] / temp[1]).real());
  283.     x2s.push_back((-temp[0] / temp[1]).imag());
  284.     fs.push_back(0);
  285.     cout << "Finding root " << n-1 << endl;
  286.     cout << "Aprox root:  " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
  287.          << x2s[n - 3] << " " << "f:"
  288.          << fs[n - 3] << endl;
  289.     InAdvGrad(x1s[n-3], x2s[n-3], E/10, k, 1);
  290.     cout << endl;
  291.     cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
  292.          << fn[n-2] << endl;
  293.     ff =ff+ ff1+ff2;
  294.     cout<<"f: "<<ff1+ff2<<endl;
  295.     ff1 = 0;
  296.     ff2 = 0;
  297.     xk3.push_back(x1n[n - 2]);
  298.     yk3.push_back(x2n[n - 2]);
  299.     fk3.push_back(fn[n - 2]);
  300.    // cout << "Count of itterations: " << itt << endl;
  301.     cout << "Count of fcalc: " << ff << endl;
  302.     //cout << "Count of gradcalc: " << gc << endl;
  303.     cout << endl;
  304.  
  305.     // Обнуляем показатели для следующего метода
  306.     for (int i = 0; i < n; i++) {
  307.         temp[i] = k[i];
  308.     }
  309.     ittv.push_back(itt);
  310.     fcv.push_back(ff);
  311.     gcv.push_back(gc);
  312.     x1n.clear();
  313.     x2n.clear();
  314.     fn.clear();
  315.     itt = 1;
  316.     fc = 1;
  317.     gc = 1;
  318.     eps = 0.01;
  319.     ff =0;
  320.     ff1 = 0;
  321.     ff2 = 0;
  322.     //Градиентный метод с дроблением шага
  323.     cout << "DIV GRAD METHOD:" << endl;
  324.     cout << "Finding root " << 1 << endl;
  325.     DivGrad(x1, x2, E, 0.05, temp, 1);
  326.     cout << endl;
  327.     cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
  328.          << fn[0] << endl;
  329.     ff =ff+ ff1+ff2;
  330.     cout<<"f: "<<ff1+ff2<<endl;
  331.     ff1 = 0;
  332.     ff2 = 0;
  333.     gornerd(x1n[0], x2n[0], temp);
  334.     xk4.push_back(x1n[0]);
  335.     yk4.push_back(x2n[0]);
  336.     fk4.push_back(fn[0]);
  337.     for (int i = 1; i < n - 2; i++) {
  338.         cout << endl;
  339.         cout << "Finding root " << i + 1 << endl;
  340.         DivGrad(x1, x2, eps, 0.05, temp, 2);
  341.         eps *= 2;
  342.         DivGrad(x1s[i - 1], x2s[i - 1], E, 0.05, k, 1);
  343.         cout << endl;
  344.         cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
  345.              << fn[i] << endl;
  346.         ff =ff+ ff1+ff2;
  347.         cout<<"f: "<<ff1+ff2<<endl;
  348.         ff1 = 0;
  349.         ff2 = 0;
  350.         gornerd(x1n[i], x2n[i], temp);
  351.         xk4.push_back(x1n[i]);
  352.         yk4.push_back(x2n[i]);
  353.         fk4.push_back(fn[i]);
  354.     }
  355.     x1s.push_back((-temp[0] / temp[1]).real());
  356.     x2s.push_back((-temp[0] / temp[1]).imag());
  357.     fs.push_back(0);
  358.     cout << "Finding root " << n-1 << endl;
  359.     cout << "Aprox root:  " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
  360.          << x2s[n - 3] << " " << "f:"
  361.          << fs[n - 3] << endl;
  362.     DivGrad(x1s[n-3], x2s[n-3], E/10, 0.05, k, 1);
  363.     cout << endl;
  364.     cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
  365.          << fn[n-2] << endl;
  366.     ff =ff+ ff1+ff2;
  367.     cout<<"f: "<<ff1+ff2<<endl;
  368.     ff1 = 0;
  369.     ff2 = 0;
  370.     xk4.push_back(x1n[n - 2]);
  371.     yk4.push_back(x2n[n - 2]);
  372.     fk4.push_back(fn[n - 2]);
  373.    // cout << "Count of itterations: " << itt << endl;
  374.     cout << "Count of fcalc: " << ff << endl;
  375.     //cout << "Count of gradcalc: " << gc << endl;
  376.     cout << endl;
  377.     for (int i = 0; i < n; i++) {
  378.         temp[i] = k[i];
  379.     }
  380.     // Обнуляем показатели для следующего метода
  381.     for (int i = 0; i < n; i++) {
  382.         temp[i] = k[i];
  383.     }
  384.     ittv.push_back(itt);
  385.     fcv.push_back(ff);
  386.     gcv.push_back(gc);
  387.     x1s.clear();
  388.     fs.clear();
  389.     fs.clear();
  390.     ff =0;
  391.     ff1 = 0;
  392.     ff2 = 0;
  393.  
  394.     x1n.clear();
  395.     x2n.clear();
  396.     fn.clear();
  397.     itt = 1;
  398.     fc = 1;
  399.     gc = 1;
  400.     eps = 0.01;
  401.  
  402.     // МНГС
  403.   /*cout << "ACCEL GRAD METHOD:" << endl;
  404.     cout << "Finding root " << 1 << endl;
  405.     AccelGrad(x1, x2, E/2, temp, 1);
  406.     cout << endl;
  407.     cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
  408.          << fn[0] << endl;
  409.     gornerd(x1n[0], x2n[0], temp);
  410.     xk5.push_back(x1n[0]);
  411.     yk5.push_back(x2n[0]);
  412.     fk5.push_back(fn[0]);
  413.     for (int i = 1; i < n - 2; i++) {
  414.         cout << endl;
  415.         cout << "Finding root " << i + 1 << endl;
  416.         AccelGrad(x1, x2, eps/2, temp, 2);
  417.         eps *= 2;
  418.         AccelGrad(x1s[i-1], x2s[i-1], E/2, k, 1);
  419.         cout << endl;
  420.         cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
  421.              << fn[i] << endl;
  422.         gornerd(x1n[i], x2n[i], temp);
  423.         xk5.push_back(x1n[i]);
  424.         yk5.push_back(x2n[i]);
  425.         fk5.push_back(fn[i]);
  426.     }
  427.     x1s.push_back((-temp[0] / temp[1]).real());
  428.     x2s.push_back((-temp[0] / temp[1]).imag());
  429.     fs.push_back(0);
  430.     cout<<endl;
  431.     cout << "Finding root " << n-1 << endl;
  432.     cout << "Aprox root:  " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
  433.          << x2s[n - 3] << " " << "f:"
  434.          << fs[n - 3] << endl;
  435.     AccelGrad(x1s[n-3], x2s[n-3], E, k, 1);
  436.     cout << endl;
  437.     cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
  438.          << fn[n-2] << endl;
  439.     cout << endl;
  440.     xk5.push_back(x1n[n - 2]);
  441.     yk5.push_back(x2n[n - 2]);
  442.     fk5.push_back(fn[n - 2]);
  443.     cout << "Count of itterations: " << itt << endl;
  444.     cout << "Count of fcalc: " << fc << endl;
  445.     cout << "Count of gradcalc: " << gc << endl;
  446.     cout << endl;
  447.     // Обнуляем показатели
  448.     ittv.push_back(itt);
  449.     fcv.push_back(fc);
  450.     gcv.push_back(gc);
  451.     x1n.clear();
  452.     x2n.clear();
  453.     fn.clear();
  454.     cout<<"essss"<<endl;
  455.     AccelGrad(1.31, -0.025, E/100, k, 1);*/
  456.  
  457.     cout<<"COORD DESCENT:"<<setw(50)<<"CONST GRAD METHOD:"<<setw(40)<<"IN ADVANCE GRAD:"<<setw(40)<< "DIV GRAD METHOD:"<<endl;
  458.     cout << setw(12) << left << "x" << setw(12) << "y" << setw(15) << "f"
  459.          << setw(12) << "x" << setw(12) << "y"  << setw(15) << "f"
  460.          << setw(12) << "x" << setw(12) << "y"  << setw(15) << "f"
  461.          << setw(12) << "x" << setw(12) << "y"  << setw(15) << "f"<<endl;
  462.     cout << setw(12) << left << xk1[0] << setw(12) << yk1[0] << setw(15) << fk1[0]
  463.          << setw(12) << xk2[0] << setw(12) << yk2[0] << setw(15) << fk2[0]
  464.          << setw(12) << xk3[0] << setw(12) << yk3[0] << setw(15) << fk3[0]
  465.          << setw(12) << xk4[0] << setw(12) << yk4[0] << setw(15) << fk4[0]
  466.           << endl;
  467.     cout << setw(12) << left << xk1[1] << setw(12) << yk1[1] << setw(15) << fk1[1]
  468.          << setw(12) << xk2[1] << setw(12) << yk2[1] << setw(15) << fk2[1]
  469.          << setw(12) << xk3[1] << setw(12) << yk3[1] << setw(15) << fk3[1]
  470.          << setw(12) << xk4[1] << setw(12) << yk4[1] << setw(15) << fk4[1]
  471.          <<  endl;
  472.     cout << setw(12) << left << xk1[2] << setw(12) << yk1[2] << setw(15) << fk1[2]
  473.          << setw(12) << xk2[2] << setw(12) << yk2[2] << setw(15) << fk2[2]
  474.          << setw(12) << xk3[2] << setw(12) << yk3[2] << setw(15) << fk3[2]
  475.          << setw(12) << xk4[2] << setw(12) << yk4[2] << setw(15) << fk4[2]
  476.          <<endl;
  477.     cout << setw(12) << left << xk1[3] << setw(12) << yk1[3] << setw(15) << fk1[3]
  478.          << setw(12) << xk2[3] << setw(12) << yk2[3] << setw(15) << fk2[3]
  479.          << setw(12) << xk3[3] << setw(12) << yk3[3] << setw(15) << fk3[3]
  480.          << setw(12) << xk4[3] << setw(12) << yk4[3] << setw(15) << fk4[3]
  481.          << endl;
  482.     //cout<<setw(20)<<"count of itt:  "<<setw(35)<<ittv[0]<<setw(35)<<ittv[1]<<setw(35)<<ittv[2]<<setw(35)<<ittv[3]<<setw(35)<<endl;
  483.     cout<<setw(20)<<"count of fcalc:  "<<setw(35)<<fcv[0]<<setw(35)<<fcv[1]<<setw(35)<<fcv[2]<<setw(35)<<fcv[3]<<setw(35)<<endl;
  484.    // cout<<setw(12)<<"count of gradcalc:  "<<setw(35)<<gcv[0]<<setw(35)<<gcv[1]<<setw(35)<<gcv[2]<<setw(35)<<gcv[3]<<setw(45)<<endl;
  485.  
  486.     return 0;
  487. }
  488.  
  489. // считает значение функции в точке
  490. double gornerf(double x, double y, complex<double> koef[]) {
  491.     return pow(gorner1f(x, y, koef).real(), 2) + pow(gorner1f(x, y, koef).imag(), 2);
  492. }
  493.  
  494. // считает значение исходного полинома в точке
  495. complex<double> gorner1f(double x, double y, complex<double> koef[]) {
  496.     complex<double> z(x, y);
  497.     complex<double> s = koef[n - 1];
  498.     for (int i = 1; i <= n - 1; ++i) {
  499.         s *= z;
  500.         s += koef[(n - 1) - i];
  501.     }
  502.     return s;
  503. }
  504.  
  505. // деление полинома на бином
  506. void gornerd(double x, double y, complex<double> *a) {
  507.     complex<double> c(x, y);
  508.     complex<double> b[n];
  509.     b[n - 1] = 0;
  510.     for (int i = n - 2; i >= 0; i--) {
  511.         b[i] = a[i + 1] + c * b[i + 1];
  512.     }
  513.     for (int j = 0; j < n; j++) {
  514.         a[j] = b[j];
  515.     }
  516. }
  517.  
  518. // координатный спуск
  519. double
  520. coordDescent(double x01, double x02, const double E, const double alp01, const double alp02, complex<double> *koef,
  521.              int k) {
  522.     double x1 = x01;
  523.     double x2 = x02;
  524.     double alp1 = alp01;
  525.     double alp2 = alp02;
  526.     int i = 2;
  527.     int j = 1;
  528.     double f1, f2, f;
  529.     f = (gornerf(x1, x2, koef));
  530.     cout << 1 << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  531.     while (abs(f) >= E) {
  532.         itt++;
  533.         int flag = -1;
  534.         f1 = gornerf(x1 + alp1, x2, koef);
  535.         f2 = gornerf(x1 - alp1, x2, koef);
  536.         if (f1 < f) {
  537.             x1 = x1 + alp1;
  538.             f = f1;
  539.             flag++;
  540.         } else if (f2 < f) {
  541.             x1 = x1 - alp1;
  542.             f = f2;
  543.             flag++;
  544.         } else
  545.             alp1 = 0.5 * alp1;
  546.         f1 = gornerf(x1, x2 + alp2, koef);
  547.         f2 = gornerf(x1, x2 - alp2, koef);
  548.         if (f1 < f) {
  549.             x2 = x2 + alp2;
  550.             f = f1;
  551.             flag++;
  552.         } else if (f2 < f) {
  553.             x2 = x2 - alp2;
  554.             f = f2;
  555.             flag++;
  556.         } else
  557.             alp2 = 0.5 * alp2;
  558.         if(flag!=-1)
  559.         {
  560.             cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  561.             i++;
  562.         }
  563.         fc = fc + 4;
  564.     }
  565.     if (k == 1) {
  566.         cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  567.         x1n.push_back(x1);
  568.         x2n.push_back(x2);
  569.         fn.push_back(f);
  570.         ff1 = 1+(i-1)*4;
  571.     }
  572.     if (k == 2) {
  573.         cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  574.         x1s.push_back(x1);
  575.         x2s.push_back(x2);
  576.         fs.push_back(f);
  577.         ff2 = 1+(i-1)*4;
  578.     }
  579.     return 0;
  580. }
  581.  
  582. //градиент функции в точке
  583. vector<double> gradient(double x, double y, complex<double> *koef) {
  584.     complex<double> div[n];
  585.     for (int i = 0; i < n - 1; i++) {
  586.         div[i] = (koef[i + 1] * (i + 1.0));
  587.     }
  588.     div[8] = ((0, 0));
  589.     complex<double> a = conj(gorner1f(x, y, koef));
  590.     complex<double> b = (gorner1f(x, y, div));
  591.     vector<double> grad;
  592.     grad.push_back((2.0 * a * b).real());
  593.     grad.push_back(-(2.0 * a * b).imag());
  594.     return grad;
  595. }
  596.  
  597. //Градиентный спуск с постоянным шагом
  598. double ConstGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k) {
  599.     double x1 = x01;
  600.     double x2 = x02;
  601.     double alp = alp0;
  602.     int i = 1;
  603.     double f;
  604.     f = gornerf(x1, x2, koef);
  605.  
  606.     while (abs(f) >= E) {
  607.         vector<double> grad = gradient(x1, x2, koef);
  608.         cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  609.         x1 = x1 - alp * grad[0];
  610.         x2 = x2 - alp * grad[1];
  611.         f = gornerf(x1, x2, koef);
  612.         fc++;
  613.         gc++;
  614.         itt++;
  615.         i++;
  616.     }
  617.     if (k == 1) {
  618.         cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  619.         x1n.push_back(x1);
  620.         x2n.push_back(x2);
  621.         fn.push_back(f);
  622.         ff1 = i;
  623.     }
  624.     if (k == 2) {
  625.         cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  626.         x1s.push_back(x1);
  627.         x2s.push_back(x2);
  628.         fs.push_back(f);
  629.         ff2=i;
  630.     }
  631.     return 0;
  632.  
  633. }
  634.  
  635. //Градиентный спуск с заранее заданным шагом
  636. double InAdvGrad(double x01, double x02, const double E, complex<double> *koef, int m) {
  637.     double x1 = x01;
  638.     double x2 = x02;
  639.     int k = 1800;
  640.     int i = 1;
  641.     double f = gornerf(x1, x2, koef);
  642.     vector<double> grad = gradient(x1, x2, koef);
  643.     while (abs(f) >= E) {
  644.         x1 = x1 - (1.0 / k) * grad[0];
  645.         x2 = x2 - (1.0 / k) * grad[1];
  646.         k++;
  647.         f = gornerf(x1, x2, koef);
  648.         grad = gradient(x1, x2, koef);
  649.         itt++;
  650.         fc++;
  651.         gc++;
  652.         i++;
  653.         cout << "x1: " << x1 << " " << "x2:" << " " << x2 << " " << "f:" << gornerf(x1, x2, koef) << endl;
  654.     }
  655.     if (m == 1) {
  656.         cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  657.         x1n.push_back(x1);
  658.         x2n.push_back(x2);
  659.         fn.push_back(f);
  660.         ff1 = i;
  661.     }
  662.     if (m == 2) {
  663.         cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  664.         x1s.push_back(x1);
  665.         x2s.push_back(x2);
  666.         fs.push_back(f);
  667.         ff2 = i;
  668.     }
  669.  
  670.     return 0;
  671.  
  672. }
  673.  
  674. //Градиентный спуск с дроблением шага
  675. double DivGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k) {
  676.     double x1 = x01;
  677.     double x2 = x02;
  678.     double alp = alp0;
  679.     double e = 0.5;
  680.     int i = 1;
  681.     int j = 1;
  682.     double f = gornerf(x1, x2, koef);
  683.     vector<double> grad = gradient(x1, x2, koef);
  684.     while (abs(f) >= E) {
  685.         cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  686.         i++;
  687.         itt++;
  688.         double x = x1 - alp * grad[0];
  689.         double y = x2 - alp * grad[1];
  690.         if ((gornerf(x, y, koef) - f) <=
  691.             -alp * e * (pow(grad[0], 2) + pow(grad[2], 2))) {
  692.             x1 = x1 - alp * grad[0];
  693.             x2 = x2 - alp * grad[1];
  694.             f = gornerf(x1, x2, koef);
  695.             j++;
  696.             grad = gradient(x1, x2, koef);
  697.             fc++;
  698.             gc++;
  699.         } else
  700.             alp = 0.5 * alp;
  701.     }
  702.     if (k == 1) {
  703.         cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  704.         x1n.push_back(x1);
  705.         x2n.push_back(x2);
  706.         fn.push_back(f);
  707.         ff1 = j;
  708.     }
  709.     if (k == 2) {
  710.         cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  711.         x1s.push_back(x1);
  712.         x2s.push_back(x2);
  713.         fs.push_back(f);
  714.         ff2 = j;
  715.     }
  716.  
  717.     return 0;
  718. }
  719.  
  720. //Золотое сечение
  721. double goldsec(double E, double a0, double b0, double x1, double x2, double alpha, complex<double> *koef) {
  722.     double a = a0, b = b0, con = (3 - sqrt(5)) / 2, c = con * (b - a) + a,
  723.             d = (1 - con) * (b - a) + a, fc = funcForOneDimMinimize(x1, x2, c, koef), fd = funcForOneDimMinimize(x1, x2,
  724.                                                                                                                  d,
  725.                                                                                                                  koef);
  726.     while (abs(b - a) > 2 * E) {
  727.         if (fc <= fd) {
  728.             b = d;
  729.             d = c;
  730.             c = con * (b - a) + a;
  731.             fd = fc;
  732.             fc = funcForOneDimMinimize(x1, x2, c, koef);
  733.         } else {
  734.             a = c;
  735.             c = d;
  736.             d = (1 - con) * (b - a) + a;
  737.             fc = fd;
  738.             fd = funcForOneDimMinimize(x1, x2, d, koef);
  739.         }
  740.     }
  741.     return (b + a) / 2;
  742. }
  743.  
  744. // Функция для одномерной минимизации
  745. double funcForOneDimMinimize(double x1, double x2, double alpha, complex<double> *koef) {
  746.     return gornerf(x1 - alpha * gradient(x1, x2, koef)[0], x2 - alpha * gradient(x1, x2, koef)[1], koef);
  747. }
  748.  
  749. //МНГС
  750. double AccelGrad(double x01, double x02, const double E, complex<double> *koef, int k) {
  751.     double x1 = x01;
  752.     double x2 = x02;
  753.     int i = 1;
  754.     double f = gornerf(x1, x2, koef);
  755.     while (abs(f) >= E) {
  756.         cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  757.         i++;
  758.         itt++;
  759.         double alp = goldsec(0.000000000000009, -1000, 1000, x1, x2, 0.0001, koef);
  760.         vector<double> grad = gradient(x1, x2, koef);
  761.         x1 = x1 - alp * grad[0];
  762.         x2 = x2 - alp * grad[1];
  763.         f = gornerf(x1, x2, koef);
  764.         fc++;
  765.         gc++;
  766.     }
  767.     if (k == 1) {
  768.         cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  769.         x1n.push_back(x1);
  770.         x2n.push_back(x2);
  771.         fn.push_back(f);
  772.     }
  773.     if (k == 2) {
  774.         cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
  775.         x1s.push_back(x1);
  776.         x2s.push_back(x2);
  777.         fs.push_back(f);
  778.     }
  779.     return 0;
  780. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement