Advertisement
Georgiy031

Untitled

Oct 22nd, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. //#include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6. typedef long long ll;
  7. typedef unsigned long long ull;
  8. #define all(x) x.begin(), x.end()
  9. #define rall(x) x.rbegin(), x.rend()
  10. #define endl '\n'
  11. #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  12. ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
  13.  
  14. double f(double x, double y) {
  15.     return x * x * x - 3 * x * y * y - x * x + y * y + x - 1;
  16. }
  17. double g(double x, double y) {
  18.     return 3 * x * x * y - y * y * y - 2 * x * y + y;
  19. }
  20.  
  21. double eps = 1e-9;
  22. double step = 0.24;
  23.  
  24. //double Xm = -10, Xp = 10;
  25. //double Ym = -10, Yp = 10;
  26.  
  27. //double Xm = -10, Xp = 10;
  28. //double Ym = -10, Yp = 0.5;
  29.  
  30. double Xm = -10, Xp = 0.5;
  31. double Ym = -10, Yp = 0.5;
  32.  
  33. double x = 0;
  34. double y = 0;
  35. double x0 = 0;
  36. double Y0 = 0;
  37. double x1 = 0;
  38. double Y1 = 0;
  39. double x2 = 0;
  40. double y2 = 0;
  41.  
  42. void initial_points0() {
  43.     double dl = step;
  44.     double curmin = 1000000.;
  45.  
  46.     while (curmin > 999999) {
  47.         for (x = Xm; x < Xp; x = x + dl)
  48.             for (y = Ym; y < Yp; y = y + dl) {
  49.  
  50.                 double fc = f(x, y);
  51.                 double gc = g(x, y);
  52.  
  53.                 if ((fc > eps) && (gc > eps)) {
  54.                     double dv = fabs(fc) + fabs(gc);
  55.  
  56.                     if (dv > curmin) continue;
  57.  
  58.                     x0 = x;
  59.                     Y0 = y;
  60.  
  61.                     curmin = dv;
  62.                 }
  63.             }
  64.         dl = dl / 2;
  65.     }
  66.     cout << " x0 = " << x0 << endl;
  67.     cout << " y0 = " << Y0 << endl;
  68. }
  69.  
  70. void initial_points1() {
  71.     double dl = step;
  72.  
  73.     double curmin = 1000000.;
  74.  
  75.     while (curmin > 999999) {
  76.         for (x = Xm; x < Xp; x = x + dl)
  77.             for (y = Ym; y < Yp; y = y + dl) {
  78.  
  79.                 double fc = f(x, y);
  80.                 double gc = g(x, y);
  81.  
  82.                 if ((fc < eps) && (gc < eps)) {
  83.  
  84.                     double dv = fabs(fc) + fabs(gc);
  85.  
  86.                     if (dv > curmin) continue;
  87.  
  88.                     x1 = x;
  89.                     Y1 = y;
  90.  
  91.                     curmin = dv;
  92.                 }
  93.             }
  94.         dl = dl / 2;
  95.     }
  96.     cout << " x1 = " << x1 << endl;
  97.     cout << " y1 = " << Y1 << endl;
  98. }
  99. void initial_points2() {
  100.  
  101.     double dl = step;
  102.  
  103.     double curmin = 1000000.;
  104.  
  105.     while (curmin > 999999) {
  106.         for (x = Xm; x < Xp; x = x + dl)
  107.             for (y = Ym; y < Yp; y = y + dl) {
  108.  
  109.                 double fc = f(x, y);
  110.                 double gc = g(x, y);
  111.  
  112.                 if ((fc > eps) && (gc < eps)) {
  113.  
  114.                     double dv = fabs(fc) + fabs(gc);
  115.  
  116.                     if (dv > curmin) continue;
  117.  
  118.                     x2 = x;
  119.                     y2 = y;
  120.  
  121.                     curmin = dv;
  122.                 }
  123.             }
  124.         dl = dl / 2;
  125.     }
  126.     //cout << "x0 = " << x0 << endl;
  127.     //cout << "Y0 = " << Y0 << endl;
  128.     //cout << "x1 = " << x1 << endl;
  129.     //cout << "Y1 = " << Y1 << endl;
  130.     cout << " x2 = " << x2 << endl;
  131.     cout << " y2 = " << y2 << endl;
  132. }
  133.  
  134. double stri(double x0_, double Y0_, double x1_, double Y1_, double x2_, double y2_) {
  135.     return (0.5 * (x0_ * (Y1_ - y2_) + x1_ * (y2_ - Y0_) + x2_ * (Y0_ - Y1_)));
  136. }
  137. signed main() {
  138.     cout << fixed << setprecision(6);
  139.     initial_points0();
  140.     cout << endl;
  141.     initial_points1();
  142.     cout << endl;
  143.     initial_points2();
  144.     cout << endl;
  145.  
  146.      //defining of the last point before the first execution of the main cycle; any values far from the initial three
  147.      //points:  
  148.  
  149.     double xls = 1000000.;
  150.     double yls = 1000000.;
  151.     double dv = 1;
  152.     double x, y;
  153.     int k = 1;
  154.     while (fabs(dv > 1e-5)) //main cycle corresponding to steps 1 – 3 of the brief description
  155.     {
  156.         // points on two surfaces calculation:
  157.         double zf0 = f(x0, Y0);
  158.         double zg0 = g(x0, Y0);
  159.         double zf1 = f(x1, Y1);
  160.         double zg1 = g(x1, Y1);
  161.         double zf2 = f(x2, y2);
  162.         double zg2 = g(x2, y2);
  163.         // the first plane coefficients: AX + BY + CZ + D = 0
  164.         double Af = (Y1 - Y0) * (zf2 - zf0) - (y2 - Y0) * (zf1 - zf0);
  165.         double Bf = (x2 - x0) * (zf1 - zf0) - (x1 - x0) * (zf2 - zf0);
  166.         double Cf = (x1 - x0) * (y2 - Y0) - (x2 - x0) * (Y1 - Y0);
  167.         double Df = -x0 * Af - Y0 * Bf - zf0 * Cf;
  168.         // the second plane coefficients:
  169.         double Ag = (Y1 - Y0) * (zg2 - zg0) - (y2 - Y0) * (zg1 - zg0);
  170.         double Bg = (x2 - x0) * (zg1 - zg0) - (x1 - x0) * (zg2 - zg0);
  171.         double Cg = (x1 - x0) * (y2 - Y0) - (x2 - x0) * (Y1 - Y0);
  172.         double Dg = -x0 * Ag - Y0 * Bg - zg0 * Cg;
  173.             // the stability checking:
  174.         double dt = Af * Bg - Ag * Bf;
  175.  
  176.         if (fabs(dt) < 0.000001) break;
  177.         // three planes intersection (z=0 by the construction):
  178.         x = (-Df * Bg + Dg * Bf) / dt;
  179.         y = (-Af * Dg + Ag * Df) / dt;
  180.         // value of both functions in the new point:
  181.         double zfc = f(x, y);
  182.         double zgc = g(x, y);
  183.         // proximity calculation (the current new point (x,y) is compared with the last one (xls,yls):
  184.         dv = fabs(x - xls) + fabs(y - yls);
  185.         // “new” last point:
  186.         xls = x;
  187.         yls = y;
  188.         // square of triangles corresponding to the discarding of one of the old points are calculated (function stri):
  189.         double s0 = stri(x1, Y1, x2, y2, x, y);
  190.         double s1 = stri(x0, Y0, x2, y2, x, y);
  191.         double s2 = stri(x0, Y0, x1, Y1, x, y);
  192.         // choice of the maximal triangle and corresponding denotation of considered points (x0,Y0), (x1,Y1), //(x2,y2) :
  193.         if ((s0 >= s1) && (s0 >= s2)){
  194.             x0 = x1;
  195.             Y0 = Y1;
  196.             x1 = x2;
  197.             Y1 = y2;
  198.             x2 = x;
  199.             y2 = y;
  200.         }
  201.         else if ((s1 >= s0) && (s1 >= s2)){
  202.             x1 = x2;
  203.             Y1 = y2;
  204.             x2 = x;
  205.             y2 = y;
  206.         }
  207.         else if ((s2 >= s0) && (s2 >= s1)){
  208.             x2 = x;
  209.             y2 = y;
  210.         }
  211.         cout << " iteration " << k++ << ": ";
  212.         cout << " x = " << xls << "   ";
  213.         cout << " y = " << yls << endl;
  214.     }//end of the main cycle
  215. }
  216.  
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement