/
levg
/
numm
Обзор
Документация
Войти
/
levg
/
numm
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
8/code/Chisl2sem1homework.cpp
102 строки
3 KB
Лев Черняховский
s
29 окт 2024, 10:38
29 окт 2024, 10:38
d9048cd
Код
Авторство
О чём код?
#include <iostream> #include <fstream> #include <cmath> #include <vector> #include <string> #define M_PI 3.141592653589793238462643383279 using namespace std; double customFunction1(double x) { return x * log(x) - 0.5; } double customFunction2(double x) { return pow(x, 3) + (x >= 0 ? 1 : -1) * pow(x, 2) + 2; } double interpolateLagrange(const vector<double>& xValues, const vector<double>& yValues, double x) { double result = 0.0; int n = xValues.size(); for (int i = 0; i < n; ++i) { double term = yValues[i]; for (int j = 0; j < n; ++j) { if (i != j) term *= (x - xValues[j]) / (xValues[i] - xValues[j]); } result += term; } return result; } vector<double> generateUniformPoints(double a, double b, int n) { vector<double> points(n); for (int i = 0; i < n; ++i) points[i] = a + i * (b - a) / (n - 1); return points; } vector<double> generateChebyshevPoints(double a, double b, int n) { vector<double> points(n); for (int i = 0; i < n; ++i) points[i] = 0.5 * ((b - a) * cos(M_PI * (2 * i + 1) / (2 * n)) + (b + a)); return points; } void computeErrorAndSave(const vector<double>& xValues, const vector<double>& yValues, double(*function)(double), const string& filename, int testPoints = 100) { ofstream outFile(filename); double a = xValues.front(), b = xValues.back(); double maxError = 0.0; for (int i = 0; i <= testPoints; ++i) { double x = a + i * (b - a) / testPoints; double actualValue = function(x); double interpolatedValue = interpolateLagrange(xValues, yValues, x); double error = fabs(actualValue - interpolatedValue); maxError = max(maxError, error); outFile << x << " " << actualValue << " " << interpolatedValue << " " << error << endl; } outFile.close(); cout << "Max error saved in " << filename << ": " << maxError << endl; } int main() { double intervalStart = 1.0, intervalEnd = 5.0; vector<int> nodeCounts; for (int n = 5; n <= 100; ++n) { nodeCounts.push_back(n); } for (int n : nodeCounts) { vector<double> uniformX = generateUniformPoints(intervalStart, intervalEnd, n); vector<double> chebyshevX = generateChebyshevPoints(intervalStart, intervalEnd, n); vector<double> uniformY(n), chebyshevY(n); for (int i = 0; i < n; ++i) { uniformY[i] = customFunction1(uniformX[i]); chebyshevY[i] = customFunction1(chebyshevX[i]); } computeErrorAndSave(uniformX, uniformY, customFunction1, "error_uniform_func1_" + to_string(n) + ".txt"); computeErrorAndSave(chebyshevX, chebyshevY, customFunction1, "error_chebyshev_func1_" + to_string(n) + ".txt"); } for (int n : nodeCounts) { vector<double> uniformX2 = generateUniformPoints(intervalStart, intervalEnd, n); vector<double> chebyshevX2 = generateChebyshevPoints(intervalStart, intervalEnd, n); vector<double> uniformY2(n), chebyshevY2(n); for (int i = 0; i < n; ++i) { uniformY2[i] = customFunction2(uniformX2[i]); chebyshevY2[i] = customFunction2(chebyshevX2[i]); } computeErrorAndSave(uniformX2, uniformY2, customFunction2, "error_uniform_func2_" + to_string(n) + ".txt"); computeErrorAndSave(chebyshevX2, chebyshevY2, customFunction2, "error_chebyshev_func2_" + to_string(n) + ".txt"); } return 0; }