fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. typedef struct {
  6. double x;
  7. double y;
  8. } Point;
  9.  
  10. double lagrangeInterpolation(Point points[], int n, double x) {
  11. int i, j;
  12. double result = 0;
  13. for (i = 0; i < n; i++) {
  14. double term = points[i].y;
  15. for (j = 0; j < n; j++) {
  16. if (i != j) {
  17. term *= (x - points[j].x) / (points[i].x - points[j].x);
  18. }
  19. }
  20. result += term;
  21. }
  22. return result;
  23. }
  24.  
  25. void calculateFiniteDifferences(Point points[], double finiteDifferences[][12], int n) {
  26. int i, j;
  27. for (i = 0; i < n; i++) {
  28. finiteDifferences[i][0] = points[i].y;
  29. }
  30.  
  31. for (j = 1; j < n; j++) {
  32. for (i = 0; i < n - j; i++) {
  33. finiteDifferences[i][j] = finiteDifferences[i + 1][j - 1] - finiteDifferences[i][j - 1];
  34. }
  35. }
  36.  
  37. printf("Таблица конечных разностей:\n");
  38. printf("x | y ");
  39. for (j = 1; j < n; j++) {
  40. printf("| Δ^%d ", j);
  41. }
  42. printf("\n");
  43.  
  44. for (i = 0; i < n; i++) {
  45. printf("%-7.2f | %-7.4f ", points[i].x, finiteDifferences[i][0]);
  46. for (j = 1; j < n - i; j++) {
  47. printf("| %-7.4f ", finiteDifferences[i][j]);
  48. }
  49. printf("\n");
  50. }
  51. }
  52.  
  53. void calculateDividedDifferences(Point points[], double dividedDifferences[], int n) {
  54. int i, j;
  55. double table[n][n];
  56.  
  57. for (i = 0; i < n; i++) {
  58. table[i][0] = points[i].y;
  59. }
  60.  
  61. for (j = 1; j < n; j++) {
  62. for (i = 0; i < n - j; i++) {
  63. table[i][j] = (table[i + 1][j - 1] - table[i][j - 1]) / (points[i + j].x - points[i].x);
  64. }
  65. }
  66.  
  67. for (i = 0; i < n; i++) {
  68. dividedDifferences[i] = table[0][i];
  69. }
  70.  
  71. printf("\nТаблица разделённых разностей:\n");
  72. printf("x | y ");
  73. for (j = 1; j < n; j++) {
  74. printf("| F[%d] ", j);
  75. }
  76. printf("\n");
  77.  
  78. for (i = 0; i < n; i++) {
  79. printf("%-7.2f | %-7.4f ", points[i].x, table[i][0]);
  80. for (j = 1; j < n - i; j++) {
  81. printf("| %-7.4f ", table[i][j]);
  82. }
  83. printf("\n");
  84. }
  85. }
  86.  
  87. double newtonInterpolation(Point points[], double dividedDifferences[], int n, double x) {
  88. double result = dividedDifferences[0];
  89. double term = 1.0;
  90. int i;
  91. for (i = 1; i < n; i++) {
  92. term *= (x - points[i - 1].x);
  93. result += dividedDifferences[i] * term;
  94. }
  95. return result;
  96. }
  97.  
  98. void saveDataToFile(Point points[], int pointCount, Point lagrangePoints[], int lagrangeCount, Point newtonPoints[], int newtonCount) {
  99. FILE *file = fopen("interpolation_data.txt", "w");
  100. if (!file) {
  101. printf("Ошибка при открытии файла.\n");
  102. return;
  103. }
  104. int i;
  105. double dividedDifferences[newtonCount];
  106. calculateDividedDifferences(newtonPoints, dividedDifferences, newtonCount);
  107.  
  108. fprintf(file, "Таблица экспериментальных данных и интерполяции:\n");
  109. fprintf(file, "x | y (реальное) | y (Лагранж) | y (Ньютон)\n");
  110. fprintf(file, "-----------------------------------------------\n");
  111.  
  112. double sumOfSquaresLagrange = 0;
  113. double sumOfSquaresNewton = 0;
  114.  
  115. for (i = 0; i < pointCount; i++) {
  116. double x = points[i].x;
  117. double actualY = points[i].y;
  118. double yLagrange = lagrangeInterpolation(lagrangePoints, lagrangeCount, x);
  119. double yNewton = newtonInterpolation(newtonPoints, dividedDifferences, newtonCount, x);
  120.  
  121. fprintf(file, "%-7.2f | %-10.4f | %-10.4f | %-10.4f\n", x, actualY, yLagrange, yNewton);
  122.  
  123. sumOfSquaresLagrange += pow(actualY - yLagrange, 2);
  124. sumOfSquaresNewton += pow(actualY - yNewton, 2);
  125. }
  126.  
  127. double rmseLagrange = sqrt(sumOfSquaresLagrange / pointCount);
  128. double rmseNewton = sqrt(sumOfSquaresNewton / pointCount);
  129.  
  130. fprintf(file, "\nСреднеквадратичная ошибка (Лагранж): %.4f\n", rmseLagrange);
  131. fprintf(file, "Среднеквадратичная ошибка (Ньютон): %.4f\n", rmseNewton);
  132.  
  133. fclose(file);
  134. printf("Данные успешно сохранены в файл interpolation_data.txt\n");
  135. }
  136.  
  137.  
  138. int main() {
  139. Point points[] = {
  140. {-3.10, 12.82}, {-2.60, 12.69}, {-2.10, 12.44}, {-1.60, 12.11},
  141. {-1.10, 11.61}, {-0.60, 11.01}, {-0.10, 10.03}, {0.40, 8.73},
  142. {0.90, 7.07}, {1.40, 4.88}, {1.90, 2.24}, {2.40, -1.05}
  143. };
  144.  
  145. Point lagrangePoints[] = {points[0], points[5], points[10]};
  146. Point newtonPoints[] = {points[0], points[3], points[7], points[10]};
  147.  
  148. int n = sizeof(points) / sizeof(points[0]);
  149.  
  150. double finiteDifferences[12][12] = {0};
  151. double dividedDifferences[12] = {0};
  152.  
  153. calculateFiniteDifferences(points, finiteDifferences, n);
  154. calculateDividedDifferences(points, dividedDifferences, n);
  155.  
  156. saveDataToFile(points, 12, lagrangePoints, 3, newtonPoints, 4);
  157. return 0;
  158. }
  159.  
  160.  
  161.  
  162.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Таблица конечных разностей:
x       | y       | Δ^1     | Δ^2     | Δ^3     | Δ^4     | Δ^5     | Δ^6     | Δ^7     | Δ^8     | Δ^9     | Δ^10     | Δ^11     
-3.10   | 12.8200 | -0.1300 | -0.1200 | 0.0400  | -0.1300 | 0.2900  | -0.8000 | 2.0000  | -4.3300 | 8.2000  | -13.6100 | 19.2400 
-2.60   | 12.6900 | -0.2500 | -0.0800 | -0.0900 | 0.1600  | -0.5100 | 1.2000  | -2.3300 | 3.8700  | -5.4100 | 5.6300  
-2.10   | 12.4400 | -0.3300 | -0.1700 | 0.0700  | -0.3500 | 0.6900  | -1.1300 | 1.5400  | -1.5400 | 0.2200  
-1.60   | 12.1100 | -0.5000 | -0.1000 | -0.2800 | 0.3400  | -0.4400 | 0.4100  | -0.0000 | -1.3200 
-1.10   | 11.6100 | -0.6000 | -0.3800 | 0.0600  | -0.1000 | -0.0300 | 0.4100  | -1.3200 
-0.60   | 11.0100 | -0.9800 | -0.3200 | -0.0400 | -0.1300 | 0.3800  | -0.9100 
-0.10   | 10.0300 | -1.3000 | -0.3600 | -0.1700 | 0.2500  | -0.5300 
0.40    | 8.7300  | -1.6600 | -0.5300 | 0.0800  | -0.2800 
0.90    | 7.0700  | -2.1900 | -0.4500 | -0.2000 
1.40    | 4.8800  | -2.6400 | -0.6500 
1.90    | 2.2400  | -3.2900 
2.40    | -1.0500 

Таблица разделённых разностей:
x       | y       | F[1]   | F[2]   | F[3]   | F[4]   | F[5]   | F[6]   | F[7]   | F[8]   | F[9]   | F[10]   | F[11]   
-3.10   | 12.8200 | -0.2600 | -0.2400 | 0.0533  | -0.0867 | 0.0773  | -0.0711 | 0.0508  | -0.0275 | 0.0116  | -0.0038 | 0.0010  
-2.60   | 12.6900 | -0.5000 | -0.1600 | -0.1200 | 0.1067  | -0.1360 | 0.1067  | -0.0592 | 0.0246  | -0.0076 | 0.0016  
-2.10   | 12.4400 | -0.6600 | -0.3400 | 0.0933  | -0.2333 | 0.1840  | -0.1004 | 0.0391  | -0.0098 | 0.0003  
-1.60   | 12.1100 | -1.0000 | -0.2000 | -0.3733 | 0.2267  | -0.1173 | 0.0364  | -0.0000 | -0.0084 
-1.10   | 11.6100 | -1.2000 | -0.7600 | 0.0800  | -0.0667 | -0.0080 | 0.0364  | -0.0335 
-0.60   | 11.0100 | -1.9600 | -0.6400 | -0.0533 | -0.0867 | 0.1013  | -0.0809 
-0.10   | 10.0300 | -2.6000 | -0.7200 | -0.2267 | 0.1667  | -0.1413 
0.40    | 8.7300  | -3.3200 | -1.0600 | 0.1067  | -0.1867 
0.90    | 7.0700  | -4.3800 | -0.9000 | -0.2667 
1.40    | 4.8800  | -5.2800 | -1.3000 
1.90    | 2.2400  | -6.5800 
2.40    | -1.0500 
Ошибка при открытии файла.