SHARE
TWEET

Untitled

a guest Apr 24th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. //variant 10
  6.  
  7. using namespace std;
  8.  
  9. const double EPSX = 0.0001;
  10. const double EPSY = 0.0001;
  11. const double EPS = 0.0001;
  12.  
  13. double getY(double x, double y)
  14. {
  15.    //return (100 * (y - x) * (y - x) + (1 - x) * (1 - x));
  16.    //return ((100 * (y - x * x) * (y - x * x)) + (1 - x) * (1 - x));
  17.    return (1 / (1 + (x - 3)*(x - 3) + (y - 2)*(y - 2)) + 2 / (1 + (x - 2)*(x - 2) + (y - 2)*(y - 2)));
  18. }
  19.  
  20. double fibbonacix(double a, double b, double y)
  21. {
  22.    double x1, x2, Fn, Fn1, Fn2, F, aold, bold, x, firstint;
  23.  
  24.    Fn2 = floor(abs(b - a) / EPSX);
  25.    vector<int> Fib(0);
  26.    int i = 1;
  27.    Fib.push_back(1);
  28.    Fib.push_back(1);
  29.    while (Fib[i] < Fn2)
  30.    {
  31.       Fib.push_back(Fib[i] + Fib[i - 1]);
  32.       i++;
  33.    }
  34.    i -= 2;
  35.  
  36.    firstint = a - b;
  37.  
  38.    while (i >= 2)
  39.    {
  40.       Fn2 = Fib[i];
  41.       Fn1 = Fib[i - 1];
  42.       Fn = Fib[i - 2];
  43.       x1 = a + Fn / Fn2 * (b - a);
  44.       x2 = a + Fn1 / Fn2 * (b - a);
  45.  
  46.       aold = a;
  47.       bold = b;
  48.  
  49.       if (getY(x1, y) < getY(x2, y))
  50.          b = x2;
  51.       if (getY(x1, y) > getY(x2, y))
  52.          a = x1;
  53.       if (getY(x1, y) == getY(x2, y))
  54.       {
  55.          a = x1;
  56.          b = x2;
  57.       }
  58.       i--;
  59.    }
  60.    
  61.    return (a + Fib[0] / Fib.back() * firstint);
  62. }
  63.  
  64. double fibbonaciy(double a, double b, double x)
  65. {
  66.    double y1, y2, Fn, Fn1, Fn2, F, aold, bold, y, firstint;
  67.  
  68.    Fn2 = floor(abs(b - a) / EPSY);
  69.  
  70.    vector<int> Fib(0);
  71.    int i = 1;
  72.    Fib.push_back(1);
  73.    Fib.push_back(1);
  74.    while (Fib[i] < Fn2)
  75.    {
  76.       Fib.push_back(Fib[i] + Fib[i - 1]);
  77.       i++;
  78.    }
  79.    i -= 2;
  80.  
  81.    firstint = a - b;
  82.  
  83.    while (i >= 2)
  84.    {
  85.       Fn2 = Fib[i];
  86.       Fn1 = Fib[i - 1];
  87.       Fn = Fib[i - 2];
  88.       y1 = a + Fn / Fn2 * (b - a);
  89.       y2 = a + Fn1 / Fn2 * (b - a);
  90.  
  91.       aold = a;
  92.       bold = b;
  93.  
  94.       if (getY(x, y1) < getY(x, y2))
  95.          b = y2;
  96.       if (getY(x, y1) > getY(x, y2))
  97.          a = y1;
  98.       if (getY(x, y1) == getY(x, y2))
  99.       {
  100.          a = y1;
  101.          b = y2;
  102.       }
  103.       i--;
  104.    }
  105.  
  106.    return (a + Fib[0] / Fib.back() * firstint);
  107. }
  108.  
  109. void intervalx(double x, double y, double *left, double *right, double delta)
  110. {
  111.    int k = 0;
  112.    double h, x1 = x, x0 = x;
  113.  
  114.    if (getY(x, y) >= getY(x + delta, y))
  115.    {
  116.       h = delta;
  117.       x1 = x + delta;
  118.    }
  119.    if (getY(x, y) > getY(x - delta, y))
  120.    {
  121.       h = -delta;
  122.       x1 = x - delta;
  123.    }
  124.    k++;
  125.  
  126.    while (getY(x, y) > getY(x1, y))
  127.    {
  128.       k++;
  129.       h *= 2;
  130.       x0 = x;
  131.       x = x1;
  132.       x1 = x + h;
  133.    }
  134.    *left = x0;
  135.    *right = x1;
  136. }
  137.  
  138. void intervaly(double x, double y, double *left, double *right, double delta)
  139. {
  140.    int k = 0;
  141.    double h, y1 = y, y0 = y;
  142.  
  143.    if (getY(x, y) >= getY(x, y + delta))
  144.    {
  145.       h = delta;
  146.       y1 = y + delta;
  147.    }
  148.    if (getY(x, y) > getY(x, y - delta))
  149.    {
  150.       h = -delta;
  151.       y1 = y - delta;
  152.    }
  153.    k++;
  154.  
  155.    while (getY(x, y) > getY(x, y1))
  156.    {
  157.       k++;
  158.       h *= 2;
  159.       y0 = y;
  160.       y = y1;
  161.       y1 = y + h;
  162.    }
  163.  
  164.    *left = y0;
  165.    *right = y1;
  166. }
  167.  
  168. void Gauss(double *x0, double *y0, double delta)
  169. {
  170.    double x1, x, y1, y, left, right;
  171.    int i = 0;
  172.    y1 = *y0;
  173.    x1 = *x0;
  174.    do
  175.    {
  176.       i++;
  177.       x = x1;
  178.       y = y1;
  179.       intervalx(x, y, &left, &right, delta);
  180.       x1 = fibbonacix(left, right, y);
  181.       intervaly(x1, y, &left, &right, delta);
  182.       y1 = fibbonaciy(left, right, x1);
  183.    } while (abs(getY(x, y) - getY(x1, y1)) > EPS && abs(x-x1) > EPSX && abs(y-y1) > EPSY);
  184.    *x0 = x;
  185.    *y0 = y;
  186.    cout << i << "\n";
  187. }
  188. //==================================================================
  189. double fibbonacixmax(double a, double b, double y)
  190. {
  191.    double x1, x2, Fn, Fn1, Fn2, F, aold, bold, x, firstint;
  192.  
  193.    Fn2 = floor(abs(b - a) / EPSX);
  194.    vector<int> Fib(0);
  195.    int i = 1;
  196.    Fib.push_back(1);
  197.    Fib.push_back(1);
  198.    while (Fib[i] < Fn2)
  199.    {
  200.       Fib.push_back(Fib[i] + Fib[i - 1]);
  201.       i++;
  202.    }
  203.    i -= 2;
  204.  
  205.    firstint = a - b;
  206.  
  207.    while (i >= 2)
  208.    {
  209.       Fn2 = Fib[i];
  210.       Fn1 = Fib[i - 1];
  211.       Fn = Fib[i - 2];
  212.       x1 = a + Fn / Fn2 * (b - a);
  213.       x2 = a + Fn1 / Fn2 * (b - a);
  214.  
  215.       aold = a;
  216.       bold = b;
  217.  
  218.       if (getY(x1, y) > getY(x2, y))
  219.          b = x2;
  220.       if (getY(x1, y) < getY(x2, y))
  221.          a = x1;
  222.       if (getY(x1, y) == getY(x2, y))
  223.       {
  224.          a = x1;
  225.          b = x2;
  226.       }
  227.       i--;
  228.    }
  229.  
  230.    return (a + Fib[0] / Fib.back() * firstint);
  231. }
  232.  
  233. double fibbonaciymax(double a, double b, double x)
  234. {
  235.    double y1, y2, Fn, Fn1, Fn2, F, aold, bold, y, firstint;
  236.  
  237.    Fn2 = floor(abs(b - a) / EPSY);
  238.  
  239.    vector<int> Fib(0);
  240.    int i = 1;
  241.    Fib.push_back(1);
  242.    Fib.push_back(1);
  243.    while (Fib[i] < Fn2)
  244.    {
  245.       Fib.push_back(Fib[i] + Fib[i - 1]);
  246.       i++;
  247.    }
  248.    i -= 2;
  249.  
  250.    firstint = a - b;
  251.  
  252.    while (i >= 2)
  253.    {
  254.       Fn2 = Fib[i];
  255.       Fn1 = Fib[i - 1];
  256.       Fn = Fib[i - 2];
  257.       y1 = a + Fn / Fn2 * (b - a);
  258.       y2 = a + Fn1 / Fn2 * (b - a);
  259.  
  260.       aold = a;
  261.       bold = b;
  262.  
  263.       if (getY(x, y1) > getY(x, y2))
  264.          b = y2;
  265.       if (getY(x, y1) < getY(x, y2))
  266.          a = y1;
  267.       if (getY(x, y1) == getY(x, y2))
  268.       {
  269.          a = y1;
  270.          b = y2;
  271.       }
  272.       i--;
  273.    }
  274.  
  275.    return (a + Fib[0] / Fib.back() * firstint);
  276. }
  277.  
  278. void intervalxmax(double x, double y, double *left, double *right, double delta)
  279. {
  280.    int k = 0;
  281.    double h, x1 = x, x0 = x;
  282.  
  283.    if (getY(x, y) <= getY(x + delta, y))
  284.    {
  285.       h = delta;
  286.       x1 = x + delta;
  287.    }
  288.    if (getY(x, y) < getY(x - delta, y))
  289.    {
  290.       h = -delta;
  291.       x1 = x - delta;
  292.    }
  293.    k++;
  294.  
  295.    while (getY(x, y) < getY(x1, y))
  296.    {
  297.       k++;
  298.       h *= 2;
  299.       x0 = x;
  300.       x = x1;
  301.       x1 = x + h;
  302.    }
  303.    *left = x0;
  304.    *right = x1;
  305. }
  306.  
  307. void intervalymax(double x, double y, double *left, double *right, double delta)
  308. {
  309.    int k = 0;
  310.    double h, y1 = y, y0 = y;
  311.  
  312.    if (getY(x, y) <= getY(x, y + delta))
  313.    {
  314.       h = delta;
  315.       y1 = y + delta;
  316.    }
  317.    if (getY(x, y) < getY(x, y - delta))
  318.    {
  319.       h = -delta;
  320.       y1 = y - delta;
  321.    }
  322.    k++;
  323.  
  324.    while (getY(x, y) < getY(x, y1))
  325.    {
  326.       k++;
  327.       h *= 2;
  328.       y0 = y;
  329.       y = y1;
  330.       y1 = y + h;
  331.    }
  332.  
  333.    *left = y0;
  334.    *right = y1;
  335. }
  336.  
  337. void Gaussmax(double *x0, double *y0, double delta)
  338. {
  339.    double x1, x, y1, y, left, right;
  340.    int i = 0;
  341.    y1 = *y0;
  342.    x1 = *x0;
  343.    do
  344.    {
  345.       i++;
  346.       x = x1;
  347.       y = y1;
  348.       intervalxmax(x, y, &left, &right, delta);
  349.       x1 = fibbonacixmax(left, right, y);
  350.       intervalymax(x1, y, &left, &right, delta);
  351.       y1 = fibbonaciymax(left, right, x1);
  352.    } while (abs(getY(x, y) - getY(x1, y1)) > EPS && abs(x - x1) > EPSX && abs(y - y1) > EPSY);
  353.    *x0 = x;
  354.    *y0 = y;
  355.    cout << i << "\n";
  356. }
  357. //==================================================================
  358.  
  359. double difx(double x, double y, double h)
  360. {
  361.    return (getY(x + h, y) - getY(x, y)) / h;
  362. }
  363.  
  364. double dify(double x, double y, double h)
  365. {
  366.    return (getY(x, y + h) - getY(x, y)) / h;
  367. }
  368.  
  369. double nabs(double x, double y)
  370. {
  371.    return sqrt(x * x + y * y);
  372. }
  373.  
  374. void fastdown(double *x0, double *y0, double lambda, double h)
  375. {
  376.    //xk+1 = xk - lambdak * grad f(xk)
  377.    double lambdak, left, right, x = *x0, y = *y0, Sx, Sy, xk = x, yk = y;
  378.    Sx = difx(x, y, h) / nabs(x, y);
  379.    Sy = dify(x, y, h) / nabs(x, y);
  380.    int i = 0;
  381.    do
  382.    {
  383.       i++;
  384.       //lambdak =
  385.       x = xk;
  386.       y = yk;
  387.       xk = x - lambda * difx(x, y, h);
  388.       yk = y - lambda * dify(x, y, h);
  389.    } while (abs(getY(x, y) - getY(xk, yk)) > EPS && abs(x - xk) > EPSX && abs(y - yk) > EPSY);
  390.    *x0 = x;
  391.    *y0 = y;
  392.    cout << i << "\n";
  393. }
  394.  
  395. void fastdownmax(double *x0, double *y0, double lambda, double h)
  396. {
  397.    //xk+1 = xk - lambdak * grad f(xk)
  398.    double lambdak, left, right, x = *x0, y = *y0, Sx, Sy, xk = x, yk = y;
  399.    Sx = difx(x, y, h) / nabs(x, y);
  400.    Sy = dify(x, y, h) / nabs(x, y);
  401.    int i = 0;
  402.    do
  403.    {
  404.       i++;
  405.       //lambdak =
  406.       x = xk;
  407.       y = yk;
  408.       xk = x + lambda * difx(x, y, h);
  409.       yk = y + lambda * dify(x, y, h);
  410.    } while (abs(getY(x, y) - getY(xk, yk)) > EPS && abs(x - xk) > EPSX && abs(y - yk) > EPSY);
  411.    *x0 = x;
  412.    *y0 = y;
  413.    cout << i << "\n";
  414. }
  415.  
  416.  
  417. void DFP()
  418. {
  419.  
  420. }
  421.  
  422. int main()
  423. {
  424.    double x, y, x0, y0;
  425.    
  426.    cin >> x >> y;
  427.    x0 = x;
  428.    y0 = y;
  429.    //Gauss(&x, &y, 0.001);
  430.    Gaussmax(&x, &y, 0.001);
  431.    cout << x << " " << y << "\n";
  432.    //fastdown(&x0, &y0, 0.001, 0.001);
  433.    fastdownmax(&x0, &y0, 0.001, 0.001);
  434.    cout << x0 << " " << y0 << "\n";
  435.  
  436.  
  437.    
  438. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top