#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
double x;
double y;
} Point;
double lagrangeInterpolation(Point points[], int n, double x) {
int i, j;
double result = 0;
for (i = 0; i < n; i++) {
double term = points[i].y;
for (j = 0; j < n; j++) {
if (i != j) {
term *= (x - points[j].x) / (points[i].x - points[j].x);
}
}
result += term;
}
return result;
}
void calculateFiniteDifferences(Point points[], double finiteDifferences[][12], int n) {
int i, j;
for (i = 0; i < n; i++) {
finiteDifferences[i][0] = points[i].y;
}
for (j = 1; j < n; j++) {
for (i = 0; i < n - j; i++) {
finiteDifferences[i][j] = finiteDifferences[i + 1][j - 1] - finiteDifferences[i][j - 1];
}
}
printf("Таблица конечных разностей:\n");
printf("x | y ");
for (j = 1; j < n; j++) {
printf("| Δ^%d ", j);
}
printf("\n");
for (i = 0; i < n; i++) {
printf("%-7.2f | %-7.4f ", points[i].x, finiteDifferences[i][0]);
for (j = 1; j < n - i; j++) {
printf("| %-7.4f ", finiteDifferences[i][j]);
}
printf("\n");
}
}
void calculateDividedDifferences(Point points[], double dividedDifferences[], int n) {
int i, j;
double table[n][n];
for (i = 0; i < n; i++) {
table[i][0] = points[i].y;
}
for (j = 1; j < n; j++) {
for (i = 0; i < n - j; i++) {
table[i][j] = (table[i + 1][j - 1] - table[i][j - 1]) / (points[i + j].x - points[i].x);
}
}
for (i = 0; i < n; i++) {
dividedDifferences[i] = table[0][i];
}
printf("\nТаблица разделённых разностей:\n");
printf("x | y ");
for (j = 1; j < n; j++) {
printf("| F[%d] ", j);
}
printf("\n");
for (i = 0; i < n; i++) {
printf("%-7.2f | %-7.4f ", points[i].x, table[i][0]);
for (j = 1; j < n - i; j++) {
printf("| %-7.4f ", table[i][j]);
}
printf("\n");
}
}
double newtonInterpolation(Point points[], double dividedDifferences[], int n, double x) {
double result = dividedDifferences[0];
double term = 1.0;
int i;
for (i = 1; i < n; i++) {
term *= (x - points[i - 1].x);
result += dividedDifferences[i] * term;
}
return result;
}
void saveDataToFile(Point points[], int pointCount, Point lagrangePoints[], int lagrangeCount, Point newtonPoints[], int newtonCount) {
FILE *file = fopen("interpolation_data.txt", "w");
if (!file) {
printf("Ошибка при открытии файла.\n");
return;
}
int i;
double dividedDifferences[newtonCount];
calculateDividedDifferences(newtonPoints, dividedDifferences, newtonCount);
fprintf(file, "Таблица экспериментальных данных и интерполяции:\n");
fprintf(file, "x | y (реальное) | y (Лагранж) | y (Ньютон)\n");
fprintf(file, "-----------------------------------------------\n");
double sumOfSquaresLagrange = 0;
double sumOfSquaresNewton = 0;
for (i = 0; i < pointCount; i++) {
double x = points[i].x;
double actualY = points[i].y;
double yLagrange = lagrangeInterpolation(lagrangePoints, lagrangeCount, x);
double yNewton = newtonInterpolation(newtonPoints, dividedDifferences, newtonCount, x);
fprintf(file, "%-7.2f | %-10.4f | %-10.4f | %-10.4f\n", x, actualY, yLagrange, yNewton);
sumOfSquaresLagrange += pow(actualY - yLagrange, 2);
sumOfSquaresNewton += pow(actualY - yNewton, 2);
}
double rmseLagrange = sqrt(sumOfSquaresLagrange / pointCount);
double rmseNewton = sqrt(sumOfSquaresNewton / pointCount);
fprintf(file, "\nСреднеквадратичная ошибка (Лагранж): %.4f\n", rmseLagrange);
fprintf(file, "Среднеквадратичная ошибка (Ньютон): %.4f\n", rmseNewton);
fclose(file);
printf("Данные успешно сохранены в файл interpolation_data.txt\n");
}
int main() {
Point points[] = {
{-3.10, 12.82}, {-2.60, 12.69}, {-2.10, 12.44}, {-1.60, 12.11},
{-1.10, 11.61}, {-0.60, 11.01}, {-0.10, 10.03}, {0.40, 8.73},
{0.90, 7.07}, {1.40, 4.88}, {1.90, 2.24}, {2.40, -1.05}
};
Point lagrangePoints[] = {points[0], points[5], points[10]};
Point newtonPoints[] = {points[0], points[3], points[7], points[10]};
int n = sizeof(points) / sizeof(points[0]);
double finiteDifferences[12][12] = {0};
double dividedDifferences[12] = {0};
calculateFiniteDifferences(points, finiteDifferences, n);
calculateDividedDifferences(points, dividedDifferences, n);
saveDataToFile(points, 12, lagrangePoints, 3, newtonPoints, 4);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCnR5cGVkZWYgc3RydWN0IHsKICAgIGRvdWJsZSB4OwogICAgZG91YmxlIHk7Cn0gUG9pbnQ7Cgpkb3VibGUgbGFncmFuZ2VJbnRlcnBvbGF0aW9uKFBvaW50IHBvaW50c1tdLCBpbnQgbiwgZG91YmxlIHgpIHsKICAgIGludCBpLCBqOwogICAgZG91YmxlIHJlc3VsdCA9IDA7CiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgZG91YmxlIHRlcm0gPSBwb2ludHNbaV0ueTsKICAgICAgICBmb3IgKGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgIGlmIChpICE9IGopIHsKICAgICAgICAgICAgICAgIHRlcm0gKj0gKHggLSBwb2ludHNbal0ueCkgLyAocG9pbnRzW2ldLnggLSBwb2ludHNbal0ueCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmVzdWx0ICs9IHRlcm07CiAgICB9CiAgICByZXR1cm4gcmVzdWx0Owp9Cgp2b2lkIGNhbGN1bGF0ZUZpbml0ZURpZmZlcmVuY2VzKFBvaW50IHBvaW50c1tdLCBkb3VibGUgZmluaXRlRGlmZmVyZW5jZXNbXVsxMl0sIGludCBuKSB7CiAgICBpbnQgaSwgajsKICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBmaW5pdGVEaWZmZXJlbmNlc1tpXVswXSA9IHBvaW50c1tpXS55OwogICAgfQoKICAgIGZvciAoaiA9IDE7IGogPCBuOyBqKyspIHsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgbiAtIGo7IGkrKykgewogICAgICAgICAgICBmaW5pdGVEaWZmZXJlbmNlc1tpXVtqXSA9IGZpbml0ZURpZmZlcmVuY2VzW2kgKyAxXVtqIC0gMV0gLSBmaW5pdGVEaWZmZXJlbmNlc1tpXVtqIC0gMV07CiAgICAgICAgfQogICAgfQoKICAgIHByaW50Zigi0KLQsNCx0LvQuNGG0LAg0LrQvtC90LXRh9C90YvRhSDRgNCw0LfQvdC+0YHRgtC10Lk6XG4iKTsKICAgIHByaW50ZigieCAgICAgICB8IHkgICAgICAgIik7CiAgICBmb3IgKGogPSAxOyBqIDwgbjsgaisrKSB7CiAgICAgICAgcHJpbnRmKCJ8IM6UXiVkICAgICAiLCBqKTsKICAgIH0KICAgIHByaW50ZigiXG4iKTsKCiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCIlLTcuMmYgfCAlLTcuNGYgIiwgcG9pbnRzW2ldLngsIGZpbml0ZURpZmZlcmVuY2VzW2ldWzBdKTsKICAgICAgICBmb3IgKGogPSAxOyBqIDwgbiAtIGk7IGorKykgewogICAgICAgICAgICBwcmludGYoInwgJS03LjRmICIsIGZpbml0ZURpZmZlcmVuY2VzW2ldW2pdKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgfQp9Cgp2b2lkIGNhbGN1bGF0ZURpdmlkZWREaWZmZXJlbmNlcyhQb2ludCBwb2ludHNbXSwgZG91YmxlIGRpdmlkZWREaWZmZXJlbmNlc1tdLCBpbnQgbikgewogICAgaW50IGksIGo7CiAgICBkb3VibGUgdGFibGVbbl1bbl07CgogICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHRhYmxlW2ldWzBdID0gcG9pbnRzW2ldLnk7IAogICAgfQoKICAgIGZvciAoaiA9IDE7IGogPCBuOyBqKyspIHsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgbiAtIGo7IGkrKykgewogICAgICAgICAgICB0YWJsZVtpXVtqXSA9ICh0YWJsZVtpICsgMV1baiAtIDFdIC0gdGFibGVbaV1baiAtIDFdKSAvIChwb2ludHNbaSArIGpdLnggLSBwb2ludHNbaV0ueCk7CiAgICAgICAgfQogICAgfQoKICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBkaXZpZGVkRGlmZmVyZW5jZXNbaV0gPSB0YWJsZVswXVtpXTsKICAgIH0KCiAgICBwcmludGYoIlxu0KLQsNCx0LvQuNGG0LAg0YDQsNC30LTQtdC70ZHQvdC90YvRhSDRgNCw0LfQvdC+0YHRgtC10Lk6XG4iKTsKICAgIHByaW50ZigieCAgICAgICB8IHkgICAgICAgIik7CiAgICBmb3IgKGogPSAxOyBqIDwgbjsgaisrKSB7CiAgICAgICAgcHJpbnRmKCJ8IEZbJWRdICAgIiwgaik7CiAgICB9CiAgICBwcmludGYoIlxuIik7CgogICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHByaW50ZigiJS03LjJmIHwgJS03LjRmICIsIHBvaW50c1tpXS54LCB0YWJsZVtpXVswXSk7CiAgICAgICAgZm9yIChqID0gMTsgaiA8IG4gLSBpOyBqKyspIHsKICAgICAgICAgICAgcHJpbnRmKCJ8ICUtNy40ZiAiLCB0YWJsZVtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KfQoKZG91YmxlIG5ld3RvbkludGVycG9sYXRpb24oUG9pbnQgcG9pbnRzW10sIGRvdWJsZSBkaXZpZGVkRGlmZmVyZW5jZXNbXSwgaW50IG4sIGRvdWJsZSB4KSB7CiAgICBkb3VibGUgcmVzdWx0ID0gZGl2aWRlZERpZmZlcmVuY2VzWzBdOwogICAgZG91YmxlIHRlcm0gPSAxLjA7CiAgICBpbnQgaTsKICAgIGZvciAoaSA9IDE7IGkgPCBuOyBpKyspIHsKICAgICAgICB0ZXJtICo9ICh4IC0gcG9pbnRzW2kgLSAxXS54KTsKICAgICAgICByZXN1bHQgKz0gZGl2aWRlZERpZmZlcmVuY2VzW2ldICogdGVybTsKICAgIH0KICAgIHJldHVybiByZXN1bHQ7Cn0KCnZvaWQgc2F2ZUR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