Advertisement
dark-s0ul

lab5.cpp

Dec 26th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. using namespace std;
  2.  
  3. #include <math.h>
  4. #include <iomanip>
  5. #include <iostream>
  6.  
  7. double Function(double x) {
  8. return log(x * x) * sin(x / 2) * exp(pow(x, 1.0/7.0));
  9. }
  10.  
  11. double L(int n, double x) {
  12. double Ln_next, Ln = x, Ln_first = 1;
  13.  
  14. if (n == 0) return Ln_first;
  15. if (n == 1) return Ln;
  16.  
  17. int i = 1;
  18. while (i < n) {
  19. Ln_next = (1.0 * (2 * i + 1) / (i + 1)) * x * Ln - (1.0 * i / (i + 1)) * Ln_first;
  20. Ln_first = Ln;
  21. Ln = Ln_next;
  22. ++i;
  23. }
  24. return Ln;
  25. }
  26.  
  27. double func(int N, double t, int k1, int k2, double a, double b) {
  28. double x = (2 * t - a - b) / (b - a);
  29. return k2 == N ? Function(t) * L(k1, x) : L(k1, x)*L(k2, x);
  30. }
  31.  
  32. double Simpson(int k1, int k2, double a, double b, int n, int N) {
  33. double sig1 = 0, sig2 = 0, y0 = func(N, a, k1, k2, a, b), yn = func(N, b, k1, k2, a, b);
  34.  
  35. double h = (b - a) / n;
  36. for (int i = 1; i < n; i++) {
  37. if (i % 2 == 0)
  38. sig2 += func(N, a + i*h, k1, k2, a, b);
  39. else
  40. sig1 += func(N, a + i*h, k1, k2, a, b);
  41. }
  42.  
  43. return h / 3 * (2 * sig2 + 4 * sig1 + y0 + yn);
  44. }
  45.  
  46. double Integral(int k1, int k2, double a, double b, int N) {
  47. double eps = 1e-8;
  48. int n = ceil((b - a) / sqrt(sqrt(eps)));
  49.  
  50. double In = Simpson(k1, k2, a, b, n, N);
  51. double I2n = Simpson(k1, k2, a, b, 2 * n, N);
  52. double r = fabs(In - I2n) / 15;
  53.  
  54. while (r > eps) {
  55. In = I2n;
  56. n *= 2;
  57. I2n = Simpson(k1, k2, a, b, n, N);
  58. r = fabs(In - I2n) / 15;
  59. }
  60. return I2n;
  61. }
  62.  
  63. double *SingleDivision(double **A, int N) {
  64. double *x = new double[N];
  65.  
  66. for (int j = 0; j < N; j++) {
  67. double m = 1.0 / A[j][j];
  68. for (int i = j; i < N + 1; i++) {
  69. A[j][i] *= m;
  70. }
  71. for (int k = j + 1; k < N; k++) {
  72. double m = A[k][j];
  73. for (int l = j; l < N + 1; l++) {
  74. A[k][l] -= A[j][l] * m;
  75. }
  76. }
  77. }
  78. for (int j = N - 1; j >= 0; j--) {
  79. double sum = 0;
  80. for (int i = N - 1; i > j; i--) {
  81. sum += A[j][i] * x[i];
  82. }
  83. x[j] = A[j][N] - sum;
  84. }
  85. return x;
  86. }
  87.  
  88. double *MakeA(double a, double b, int N) {
  89. double ** A = new double*[N + 1];
  90. for (int i = 0; i < N + 1; i++)
  91. A[i] = new double[N + 1];
  92.  
  93. for (int i = 0; i < N; i++) {
  94. for (int j = 0; j < N + 1; j++) {
  95. A[i][j] = A[j][i] = Integral(i, j, a, b, N);
  96. }
  97. A[i][N] = Integral(i, N, a, b, N);
  98. }
  99.  
  100. return SingleDivision(A, N);
  101.  
  102. }
  103.  
  104. double Pm(double x, double a, double b, double *A, int n) {
  105. double t = (2 * x - b - a) / (b - a);
  106. double y = 0;
  107. for (int j = 0; j < n; j++) {
  108. y += A[j] * L(j, t);
  109. }
  110. return y;
  111. }
  112.  
  113. int main() {
  114. double a = 1, b = 35;
  115. double eps = 1e-3;
  116.  
  117. int k = (b - a) * 2;
  118. double step = (b - a) / (double)k;
  119. int n = 2;
  120.  
  121. while (true) {
  122. n *= 2;
  123. double deviation = 0;
  124. double x = a;
  125. double *A = MakeA(a, b, n);
  126. for (int i = 0; i <= k; ++i) {
  127. double y = Function(x) - Pm(x, a, b, A, n);
  128. deviation += y * y;
  129. x += step;
  130. }
  131.  
  132. if (sqrt(deviation / (k + 1)) <= eps) break;
  133. }
  134.  
  135. cout << "N = " << n << endl << endl;
  136.  
  137. double *A = MakeA(a, b, n), x = a;
  138. cout << "X P(x)" << endl;
  139. for (int i = 0; i <= k; i++, x += step) {
  140. printf("%-4f %.8f\n", x, Pm(x, a, b, A, n));
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement