Загрузка данных


#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <locale>
#include <iomanip>
using namespace std;

double funct(double x)
{
	return 1.7 * pow(x, 1.0/3.0) - cos(0.4 - 0.7 * x);
}

double lagrange(double x, vector<double> X, vector<double> Y)
{
	double res = 0.0;
	double term;
	int n = X.size();
	int i, j;

	for (i = 0; i < n; i++)
	{
		term = Y[i];
		for (j = 0; j < n; j++)
		{
			if (i != j) term = term * (x - X[j]) / (X[i] - X[j]);
		}
		res += term;
	}
	return res;
}

vector<int> nodes(double x, vector<double> X)
{
	int n = X.size();
	int i;

	vector<int> index(n);
	for (i = 0; i < n; i++)
	{
		index[i] = i;
	}

	sort(index.begin(), index.end(), [&](int a, int b)
		{
			return fabs(X[a] - x) < fabs(X[b] - x);
		});
	vector<int> points(index.begin(), index.begin() + 3);
	sort(points.begin(), points.end());

	return points;
}

int main()
{
	setlocale(LC_ALL, "Russian");

	vector<double> arrX = {2.6, 3.3, 4.7, 6.1, 7.5, 8.2, 9.6};
	vector<double> arrY = {2.1874, 2.8637, 3.8161, 3.8524, 3.1905, 2.8409, 2.6137};

	double x;
	cin >> x;

	cout << '\n';

	vector<int> node = nodes(x, arrX);
	vector<double> nX, nY;
	for (int i : node)
	{
		nX.push_back(arrX[i]);
		nY.push_back(arrY[i]);
		cout << "Узел интерполяции по x: " << arrX[i] << ", его значение f(x): " << arrY[i] << endl;
	}

	cout << '\n';

	double approxval = lagrange(x, nX, nY);
	double exactval = funct(x);
	double absolute_Rn = fabs(exactval - approxval);

	cout << "Наша точка: " << x << endl;
	cout << "Приближенное значение по Лагранжу: " << approxval << endl;
	cout << "Точное значение по функции: " << setprecision(5) << exactval << endl;
	cout << "Абсолютная погрешность: " << setprecision(2) << absolute_Rn << endl;
	if (fabs(exactval) > 1e-12) cout << "Относительная погрешность: " << setprecision(2) << absolute_Rn / fabs(exactval) * 100 << "%" << endl;

	return 0;
}