Advertisement
dark-s0ul

lab5

Jan 7th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cmath>
  3.  
  4. double a = 0, b = 1.2;
  5. double eps = 1e-3;
  6. int k = (b - a) * 50;
  7. double step = (b - a) / (double) k;
  8. double sqrError = eps * eps * (k + 1);
  9.  
  10. double Function(double x) {
  11.     return 10 * x * x * cosh(x) * sin(13 * x);
  12. }
  13.  
  14. double Chebyshev(double x, int n) {
  15.     if (n == 0) return 1;
  16.  
  17.     double tn = x;
  18.     double tnm1 = 1;
  19.  
  20.     for (int i = 1; i < n; i++) {
  21.         double tnp1 = 2 * x * tn - tnm1;
  22.  
  23.         tnm1 = tn;
  24.         tn = tnp1;
  25.     }
  26.  
  27.     return tn;
  28. }
  29.  
  30. double Polinom(double x, double *ak, int N) {
  31.     double t = (2 * x - b - a) / (b - a);
  32.     double s = 0;
  33.     for (int i = 0; i < N; i++) {
  34.         s += ak[i] * Chebyshev(t, i);
  35.     }
  36.     return s;
  37. }
  38.  
  39. double F(double x, int k1, int k2, int N) {
  40.     double t = (2 * x - (a + b)) / (b - a);
  41.  
  42.     if (k2 == N) {
  43.         return Chebyshev(t, k1) * Function(x);
  44.     } else {
  45.         return Chebyshev(t, k1) * Chebyshev(t, k2);
  46.     }
  47. }
  48.  
  49. double Simpson(int k1, int k2, int n, int N) {
  50.     double s1 = 0;
  51.     double s2 = 0;
  52.     double h = (b - a) / (double) n;
  53.  
  54.     for (int i = 1; i < n; i++) {
  55.         double x = a + i * h;
  56.  
  57.         if (i % 2 == 0) {
  58.             s2 += F(x, k1, k2, N);
  59.         } else {
  60.             s1 += F(x, k1, k2, N);
  61.         }
  62.     }
  63.  
  64.     return h / 3.0 * (2.0 * s2 + 4.0 * s1 + F(a, k1, k2, N) + F(b, k1, k2, N));
  65. }
  66.  
  67. double Runge(int k1, int k2, int N) {
  68.     int n = (b - a) * 100;
  69.    
  70.     double I2n = Simpson(k1, k2, n, N);
  71.  
  72.     loop:
  73.         double In = I2n;
  74.         I2n = Simpson(k1, k2, n *= 2, N);
  75.         if (fabs(In - I2n) >= 15e-8) goto loop;
  76.  
  77.     return I2n;
  78. }
  79.  
  80. void Ak(double *ak, int N) {
  81.     double matrix[N][N + 1];
  82.  
  83.     for (int i = 0; i < N; i++) {
  84.         for (int j = 0; j <= N; j++) {
  85.             matrix[i][j] = Runge(i, j, N);
  86.         }
  87.     }
  88.  
  89.     for (int j = 0; j < N; j++) {
  90.         double m = matrix[j][j];
  91.         for (int i = j; i <= N; i++) {
  92.             matrix[j][i] /= m;
  93.         }
  94.         for (int k = j + 1; k < N; k++) {
  95.             double m = matrix[k][j];
  96.             for (int l = j; l <= N; l++) {
  97.                 matrix[k][l] -= matrix[j][l] * m;
  98.             }
  99.         }
  100.     }
  101.  
  102.     for (int i = N - 1; i >= 0; i--) {
  103.         double s = 0;
  104.  
  105.         for (int j = i + 1; j < N; j++) {
  106.             s += matrix[i][j] * ak[j];
  107.         }
  108.         ak[i] = matrix[i][N] - s;
  109.     }
  110. }
  111.  
  112. int main() {
  113.     int N = 2;
  114.  
  115.     loop:
  116.         double error = 0;
  117.         double ak[N *= 2];
  118.         Ak(ak, N);
  119.         for (int i = 0; i <= k; i++) {
  120.             double x = a + i * step;
  121.             error += pow(Function(x) - Polinom(x, ak, N), 2);
  122.         }
  123.  
  124.         if (error > sqrError) goto loop;
  125.  
  126.     printf("N = %i\n", N);
  127.  
  128.     for (int i = 0; i <= k; i++) {
  129.         double x = a + i * step;
  130.  
  131.         printf("%-.2f\t%.8f\n", x, Polinom(x, ak, N)); 
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement