Vladislav_Bezruk

Untitled

Dec 17th, 2021 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. struct Point {
  7.     double x, y;
  8.  
  9.     Point() {x = y = 0;}
  10.     Point(double _x, double _y) : x(_x), y(_y) {};
  11. };
  12.  
  13. double checkEps(Point* s1, Point* s2, int n) {
  14.     double eps = -1, curEps;
  15.    
  16.     for (int i = 0; i < n; i++) {
  17.         curEps = fabs(s1[i].y - s2[i * 2].y) / 15;
  18.         eps = max(eps, curEps);
  19.     }
  20.     return eps;
  21. }
  22.  
  23. double pi(double x) {return x;}
  24.  
  25. double qi(double x) {return 1;}
  26.  
  27. double fi(double x) {return x + 1;}
  28.  
  29. double ai(double x, double h) {return 1 - h * pi(x) / 2;}
  30.  
  31. double bi(double x, double h) {return -(h * h * qi(x) - 2);}
  32.  
  33. double ci(double x, double h) {return 1 + h * pi(x) / 2;}
  34.  
  35. double di(double x, double h) {return h * h * fi(x);}
  36.  
  37. Point* createPoint(double xa, double xb, double a1, double a2, double a, double b1, double b2, double b, int n) {
  38.     Point* res = new Point[n + 1];
  39.     double pp[n + 1], qq[n + 1], h = (xb - xa) / n, b0 = -(h * a1 - a2), c0 = a2, d0 = h * a, an = -b2, bn = -(h * b1 + b2), dn = h * b;
  40.     int i = 0;
  41.    
  42.     pp[0] = c0 / b0; qq[0] = -d0 / b0;
  43.    
  44.     for (double x = xa; i < n; i++, x += h) {
  45.         pp[i + 1] = ci(x, h) / (bi(x, h) - ai(x, h) * pp[i]);
  46.         qq[i + 1] = (ai(x, h) * qq[i] - di(x, h)) / (bi(x, h) - ai(x, h) * pp[i]);
  47.     }
  48.     res[n] = Point(xb, (an * qq[n] - dn) / (bn - an * pp[n]));
  49.    
  50.     for (i; i > 0; i--)
  51.         res[i - 1] = Point(xb - h * (n - i + 1), pp[i] * res[i].y + qq[i]);
  52.        
  53.     return res;
  54. }
  55.  
  56. void solve(double xa, double xb, double a1, double a2, double a, double b1, double b2, double b, double eps) {
  57.     Point *s1, *s2;
  58.     int n = 10;
  59.    
  60.     do {
  61.         s1 = createPoint(xa, xb, a1, a2, a, b1, b2, b, n);
  62.         n *= 2;
  63.         s2 = createPoint(xa, xb, a1, a2, a, b1, b2, b, n);
  64.     } while (checkEps(s1, s2, n / 2) > eps);
  65.    
  66.     cout << "n = " << n << endl;
  67.     cout << endl;
  68.     cout.precision(5);
  69.     for (int i = 0; i <= n; i++) {
  70.         cout << "x[" << i << "] = " << s2[i].x << "\ty[" << i << "] = " << s2[i].y << endl;
  71.     }
  72. }
  73.  
  74. int main() {
  75.     solve(0.5, 0.8, 1., 2., 1., 0., 1., 1.2, 1e-4);
  76.    
  77.     return 0;
  78. }
Add Comment
Please, Sign In to add comment