dark-s0ul

Untitled

Dec 24th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <cmath>
  4. #include <iomanip>
  5. #include <iostream>
  6.  
  7. double f(double x){
  8.     return pow(M_E, (pow(x - 15, 0.25))) * sin(1.2 * x);
  9. }
  10.  
  11. double L(int n, double x){
  12.     double Ln = x, Ln_first = 1;
  13.  
  14.     if (n == 0) return Ln_first;
  15.  
  16.     int i = 1;
  17.     for (int i = 1; i < n; i++) {
  18.         double Ln_next = (1.0 * (2 * i + 1) / (double)(i + 1)) * x * Ln - (1.0 * i / (double)(i + 1)) * Ln_first;
  19.         Ln_first = Ln;
  20.         Ln = Ln_next;
  21.     }
  22.     return Ln;
  23. }
  24.  
  25. double func(int N, double t, int k1, int k2, double a, double b){
  26.     double x = (2 * t - a - b) / (b - a);
  27.     return L(k1, x) * (k2 == N ? f(t) : L(k2, x));
  28. }
  29.  
  30. double simpson(int k1, int k2, double a, double b, int n, int N) {
  31.     double sig1 = 0, sig2 = 0, h = (b - a) / n, x = a;
  32.     for (int i = 1; i < n; i++) {
  33.         x += h;
  34.         if (i % 2 == 0) sig2 += func(N, x, k1, k2, a, b);
  35.         else sig1 += func(N, x, k1, k2, a, b);
  36.     }
  37.  
  38.     return h / 3 * (2 * sig2 + 4 * sig1 + func(N, a, k1, k2, a, b) + func(N, b, k1, k2, a, b));
  39. }
  40.  
  41. double Integral(int k1, int k2, double a, double b, int N) {
  42.     double eps = 1e-8;
  43.  
  44.     int n = 2.0 * ceil((b - a) / sqrt(sqrt(eps)) / 2.0);
  45.     double In, I2n = simpson(k1, k2, a, b, n, N);
  46.     do {
  47.         In = I2n;
  48.         I2n = simpson(k1, k2, a, b, n *= 2, N);
  49.     } while (fabs(In - I2n) / 15.0 > eps);
  50.  
  51.     return I2n;
  52. }
  53.  
  54. double* complete_pivoting(double *matrix, int N){
  55.     int p, q, current_line = 0;
  56.     double t[(N + 1) * (N + 1)], M[N], *x = new double[N];
  57.  
  58.     for (int k = 0; k < N - 1; k++){
  59.         double max = 0;
  60.         for (int i = 0; i < N; i++) {
  61.             for (int j = 0; j < N; j++) {
  62.                 float a = fabs(matrix[i * (N + 1) + j]);
  63.                 if (a > max) {
  64.                     max = a;
  65.                     p = i;
  66.                     q = j;
  67.                 }
  68.             }
  69.         }
  70.                
  71.  
  72.         for (int j = 0; j < N + 1; j++) {
  73.             t[current_line * (N + 1) + j] = matrix[p * (N + 1) + j];
  74.         }
  75.         current_line++;
  76.  
  77.         for (int i = 0; i < N; i++) {
  78.             M[i] = -matrix[i * (N + 1) + q] / max;
  79.         }
  80.  
  81.         for (int i = 0; i < N; i++) {
  82.             for (int j = 0; j < N + 1; j++) {
  83.                 if (i != p) {
  84.                     matrix[i * (N + 1) + j] += matrix[p * (N + 1) + j] * M[i];
  85.                 }
  86.             }
  87.         }
  88.  
  89.         for (int j = 0; j < N + 1; j++) {
  90.             matrix[p * (N + 1) + j] = 0;
  91.         }
  92.  
  93.         for (int i = 0; i < N; i++) {
  94.             matrix[i * (N + 1) + q] = 0;
  95.         }
  96.     }
  97.  
  98.     for (int i = 0; i < N; i++) {
  99.         if (matrix[i * (N + 1) + N] != 0) {
  100.             p = i;
  101.         }
  102.     }
  103.  
  104.     for (int j = 0; j < N + 1; j++) {
  105.         t[current_line * (N + 1) + j] = matrix[p * (N + 1) + j];
  106.     }
  107.  
  108.     for (int j = 0; j < N; j++) {
  109.         x[j] = 0;
  110.     }
  111.  
  112.     for (int i = N - 1; i >= 0; i--){
  113.         double R = t[i * (N + 1) + N];
  114.         for (int j = 0; j < N; j++) {
  115.             if ((t[i * (N + 1) + j] != 0) && (x[j] != 0)) {
  116.                 R -= t[i * (N + 1) + j] * x[j];
  117.             }
  118.         }
  119.         for (int j = 0; j < N; j++) {
  120.             if ((t[i * (N + 1) + j] != 0) && (x[j] == 0)) {
  121.                 x[j] = R / t[i * (N + 1) + j];
  122.             }
  123.         }
  124.     }
  125.  
  126.     return x;
  127. }
  128.  
  129. double *Ak(double a, double b, int N){
  130.     double A[(N + 1) * (N + 1)];
  131.  
  132.     for (int i = 0; i < N; i++) {
  133.         for (int j = 0; j < N + 1; j++) {
  134.             A[i * (N + 1) + j] = A[j * (N + 1) + i] = Integral(i, j, a, b, N);
  135.         }
  136.         A[i * (N + 1) + N] = Integral(i, N, a, b, N);
  137.     }
  138.  
  139.     return complete_pivoting(A, N);
  140. }
  141.  
  142. double Pm(double x, double a, double b, double *ak, int n){
  143.     double t = (2 * x - b - a) / (b - a);
  144.     double y = 0;
  145.     for (int j = 0; j < n; ++j) {
  146.         y += ak[j] * L(j, t);
  147.     }
  148.     return y;
  149. }
  150.  
  151. int main(){
  152.     double a = 20, b = 30, eps = 0.01;
  153.  
  154.     int k = (b - a) * 2, n = 2;
  155.     double step = (b - a) / k, err;
  156.     do {
  157.         n *= 2;
  158.         err = 0;
  159.         double x = a;
  160.         double *ak = Ak(a, b, n);
  161.         for (int i = 0; i <= k; ++i){
  162.             double delta = f(x) - Pm(x, a, b, ak, n);
  163.             err += delta * delta;
  164.             x += step;
  165.         }
  166.     } while (sqrt(err / (k + 1)) > eps);
  167.  
  168.     cout << "N = " << n << endl << endl;
  169.  
  170.     double *ak = Ak(a, b, n);
  171.     cout << "   X          P(x)" << endl;
  172.     for (double x = a; x <= b; x += step){
  173.         printf("%7.2f%15.8f\n", x, Pm(x, a, b, ak, n));
  174.     }
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment