Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include "pch.h"
  2. #define _USE_MATH_DEFINES
  3. #include <SDKDDKVer.h>
  4. #include <cmath>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <windows.h>
  9.  
  10. using namespace std;
  11. const double step = 0.2;
  12. int npoint = trunc(3.14 / step);
  13. const int maxChebDat = 12;
  14. double pi = 3.14;
  15. double e = 2.72;
  16.  
  17. int chebdat[maxChebDat][maxChebDat / 2 + 1] =
  18. { { 1, 0, 0, 0, 0, 0, 0 },
  19. { 2, -1, 0, 0, 0, 0, 0 },
  20. { 4, -3, 0, 0, 0, 0, 0 },
  21. { 8, -8, 1, 0, 0, 0, 0 },
  22. { 16, -20, 5, 0, 0, 0, 0 },
  23. { 32, -48, 18, -1, 0, 0, 0 },
  24. { 64, -112, 56, -7, 0, 0, 0 },
  25. { 128, -256, 160, -32, 1, 0, 0 },
  26. { 256, -576, 432, -120, 9, 0, 0 },
  27. { 512, -1280, 1120, -400, 50, -1, 0 },
  28. { 1024, -2816, 2816, -1232, 220, -11, 0 },
  29. { 2048, -6144, 6912, -3584, 840, -72, 1 }
  30. };
  31.  
  32. // Вычисление исходной функции
  33. double F(double z) {
  34. //return 1 * pow(sin(1.3 * z), 2);
  35. //return 5 * z*pow(e, -z);
  36. return 2 * pow(sin(1.2 * z), 2);
  37. }
  38.  
  39. //double F0(double z) {
  40. // return sqrt(z * sin(z));
  41. //}
  42.  
  43. //Весовая функция
  44. double F11(double z) {
  45. return sqrt(1 - pow(((2 * z - pi) / pi), 2));
  46. }
  47. // погрешность абсолютная (среднеквадратическая ошибка)
  48. double delta(double* Q1, double* F1, int n) {
  49. double sum;
  50. for (int i = 0; i < n; i++) {
  51. sum = pow((Q1[i] - F1[i]), 2);
  52. }
  53. return sqrt(sum / n);
  54. }
  55. // погрешность относительная
  56. double sigma(double delta, double F0max) {
  57. return delta / F0max * 100;
  58. }
  59. // Вычисление полинома Чебышева
  60. double cheb(double x, int st) {
  61. double xst;
  62. int nst;
  63. double sum;
  64. int n;
  65. x = (2 * x - pi) / pi;
  66. if (st == 0) {
  67. return 1;
  68. }
  69. sum = 0;
  70. nst = st % 2;
  71. if (nst == 0) {
  72. xst = 1;
  73. }
  74. else {
  75. xst = x;
  76. }
  77. n = (st + 2) / 2;
  78. while (nst <= st) {
  79. sum = sum + chebdat[st - 1][n - 1] * xst;
  80. nst += 2;
  81. xst = xst * pow(x, 2);
  82. n--;
  83. }
  84. return sum;
  85. }
  86.  
  87. int main(int argc, char *argv[]) {
  88. int j, n;
  89. double mnog, c, q, e, F0max = 0;
  90. double a[20];
  91. double* Q1 = (double *)malloc((npoint + npoint) * sizeof(double));
  92. double* F1 = (double *)malloc((npoint + npoint) * sizeof(double));
  93. printf("%d \n", npoint);
  94. printf("Enter n : ");
  95. scanf_s("%d", &n);
  96. //first part, вычисление А
  97. a[0] = 0;
  98. c = step;
  99. while (c <= 3) {
  100. a[0] = a[0] + F(c)*step * 2 / (F11(c) * pow(pi, 2));
  101. c = c + step;
  102. }
  103. for (int i = 1; i < n; i++) {
  104. a[i] = 0;
  105. c = 0;
  106. c = c + step;
  107. while (c <= pi) {
  108. mnog = cheb(c, i);
  109. a[i] = a[i] + F(c)*mnog*step * 4 / (F11(c) * pow(pi, 2));
  110. c = c + step;
  111. }
  112. }
  113. printf("\n");
  114. c = 0;
  115. //second part, вычисление аппроксимирующей функции по Чебышеву
  116. double delta = 0;
  117. while (c <= pi) {
  118. q = 0.0;
  119. for (int i = 0; i < n; i++) {
  120. mnog = cheb(c, i);
  121. q = q + a[i] * mnog;
  122. }
  123. printf("|Q = %5.3f", q);
  124. printf("| c = %5.3f", c);
  125. printf("| F = %5.3f", F(c));
  126. e = c / step;
  127. j = round(e);
  128. Q1[j] = q;
  129. F1[j] = F(c);
  130. printf("| q1 = %5.3f", Q1[j]);
  131. printf("| f1 = %5.3f|", F1[j]);
  132. printf("\n");
  133. c = c + step;
  134. delta = delta + pow((Q1[j] - F1[j]), 2);
  135. }
  136. Q1[j + 1] = q;
  137. double d = sqrt(delta / npoint);
  138. double sig = 100 * delta / 2.913;
  139. printf("\n");
  140. printf("delta = %5.3f\n", d);
  141. printf("sigma = %5.3f%%\n", sig);
  142. FILE *filex, *filey, *fileq;
  143. fopen_s(&filex, "C:\\x.txt", "w");
  144. fopen_s(&filey, "C:\\y.txt", "w");
  145. fopen_s(&fileq, "C:\\q.txt", "w");
  146. double x = 0.0, y = 0.0;
  147. fprintf(filex, "x = [");
  148. for (double i = 0; i < npoint; i++) {
  149. x = i / npoint;
  150. fprintf(filex, "%f", x);
  151. fprintf(filex, ", ");
  152. }
  153. fprintf(filex, "]");
  154. fclose(filex);
  155. x = 0.0, y = 0.0;
  156. fprintf(filey, "y = [");
  157. for (int i = 0; i < npoint; i++) {
  158. y = F1[i];
  159. fprintf(filey, "%f", y);
  160. fprintf(filey, ", ");
  161. }
  162. fprintf(filey, "]");
  163. fclose(filey);
  164. x = 0.0, y = 0.0;
  165. fprintf(fileq, "q = [");
  166. for (int i = 0; i < npoint; i++) {
  167. y = Q1[i];
  168. fprintf(fileq, "%f", y);
  169. fprintf(fileq, ", ");
  170. }
  171. fprintf(fileq, "]");
  172. fclose(fileq);
  173. system("pause");
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement