Advertisement
dark-s0ul

lab5.cpp

Dec 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <cmath>
  6.  
  7. double f(double x) {
  8.     return 1 / x - 0.1 * x * x * sin(2 * x);
  9. }
  10.  
  11. double *single_division(double *A, int N) {
  12.     double *x = new double[N];
  13.  
  14.     for (int j = 0; j < N; j++) {
  15.         double m = 1.0 / A[j * (N + 1) + j];
  16.         for (int i = j; i < N + 1; i++) {
  17.             A[j * (N + 1) + i] *= m;
  18.         }
  19.         for (int k = j + 1; k < N; k++) {
  20.             double m = A[k * (N + 1) + j];
  21.             for (int l = j; l < N + 1; l++) {
  22.                 A[k * (N + 1) + l] -= A[j * (N + 1) + l] * m;
  23.             }
  24.         }
  25.     }
  26.     for (int j = N - 1; j >= 0; j--) {
  27.         double s = 0;
  28.         for (int i = N - 1; i > j; i--) {
  29.             s += A[j * (N + 1) + i] * x[i];
  30.         }
  31.         x[j] = A[j * (N + 1) + N] - s;
  32.     }
  33.     return x;
  34. }
  35.  
  36. double chebyshev(double x, int n) {
  37.     double Tn = x, Tn_minus_1 = 1;
  38.     if (n == 0) return Tn_minus_1;
  39.  
  40.     for (int i = 1; i < n; i++) {
  41.         double T = 2 * x * Tn - Tn_minus_1;
  42.  
  43.         Tn_minus_1 = Tn;
  44.         Tn = T;
  45.     }
  46.  
  47.     return Tn;
  48. }
  49.  
  50. double func(double x, double a, double b, int k1, int k2, int N) {
  51.     double t = (2 * x - (a + b)) / (b - a);
  52.     return chebyshev(t, k1) * (k2 == N ? f(x): chebyshev(t, k2));
  53. }
  54.  
  55. double simpson(double a, double b, int k1, int k2, int n, int N) {
  56.     double s1 = 0, s2 = 0, h = (b - a) / n;
  57.     for (int i = 1; i < n; i++) {
  58.         if (i % 2 == 0) s2 += func(a + i * h, a, b, k1, k2, N);
  59.         else s1 += func(a + i * h, a, b, k1, k2, N);
  60.     }
  61.  
  62.     return h / 3 * (2 * s2 + 4 * s1 + func(a, a, b, k1, k2, N) + func(b, a, b, k1, k2, N));
  63. }
  64.  
  65. double integral(double a, double b, int k1, int k2, int N) {
  66.     int n = (int((b - a) / sqrt(1e-3)) >> 1) << 1;
  67.    
  68.     double I2n = simpson(a, b, k1, k2, n, N), In = 0;
  69.     double error = fabs(In - I2n) / 3;
  70.  
  71.     while (error > 1e-3) {
  72.         In = I2n;
  73.         I2n = simpson(a, b, k1, k2, n *= 2, N);
  74.         error = fabs(In - I2n) / 3;
  75.     }
  76.     return I2n;
  77. }
  78.  
  79. double *Ak(double a, double b, int N) {
  80.     double A[(N + 1) * (N + 1)];
  81.  
  82.     for (int i = 0; i < N; i++) {
  83.         for (int j = 0; j < N + 1; j++) {
  84.             A[i * (N + 1) + j] = A[j * (N + 1) + i] = integral(a, b, i, j, N);
  85.         }
  86.         A[i * (N + 1) + N] = integral(a, b, i, N, N);
  87.     }
  88.  
  89.     return single_division(A, N);
  90. }
  91.  
  92. double P(double x, double a, double b, double *A, int N) {
  93.     double t = (2 * x - b - a) / (b - a);
  94.     double s = 0;
  95.     for (int i = 0; i < N; i++) {
  96.         s += A[i] * chebyshev(t, i);
  97.     }
  98.     return s;
  99. }
  100.  
  101. int main() {
  102.     double a = 2, b = 11;
  103.     double eps = 1e-2;
  104.     double step = 0.1;
  105.     int k = k = (int)(b - a) / step;
  106.     int N = 2;
  107.  
  108.     for(;;) {
  109.         N *= 2;
  110.         double error = 0;
  111.         double x = a;
  112.         for (int i = 0; i <= k; ++i) {
  113.             auto A = Ak(a, b, N);
  114.             double delta = f(x) - P(x, a, b, A, N);
  115.             error += delta * delta;
  116.             x += step;
  117.             delete[] A;
  118.         }
  119.         if (sqrt(error / (k + 1)) <= eps) break;
  120.     };
  121.  
  122.     cout << "N = " << N << endl;
  123.  
  124.     auto A = Ak(a, b, N);
  125.     for (double x = a; x <= b; x += step) {
  126.         printf("%-5.1f  %.8f\n", x, P(x, a, b, A, N)); 
  127.     }
  128.     delete[] A;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement