拉格朗日插值多项式(附C++代码)

概念

一般的,若已知y = f(x),在互不相同的n+1个点{x_{0,}}{x_1},{x_{2,}}...,{x_n}处的函数值{y_{0,}}{y_1},{y_{2,}}...,{y_n},则可以考虑构造一个过这n+1个点的、次数不超过n的多项式y = P(x)代替y。

实际应用

拉格朗插值法主要用于求解给定离散数据,拟合出相应多项式,可以表示出过相应的点的一个多项式

拉格朗日公式

P(x) = \sum\limits_{k = 0}^n {​{l_k}(x){y_k}} ,其中{l_i}(x) = \prod\limits_{\begin{array}{*{20}{c}} {j = 0}\\ {j \ne i} \end{array}}^n {\frac{​{x - {x_j}}}{​{​{x_i} - {x_j}}}}(0 \le j \le n)

 C++实现代码

注释:本代码可以实现任意组所给离散点的拟合

void enter(vector<pair<double, double>> &x)  //录入信息
{
	do{
		static int n = 0;
		cout << "是否输入第" << n + 1 << "组数据,Y继续输入,N退出输入数据" << endl;
		string u;
		cin >> u;
		if (u == "Y")
		{
			n++;
			double dx;        //中间变量,x
			double dy;        //中间变量y值
			cout << "请输入x,y值,中间用空格隔开" << endl;
			cin >> dx;
			cin >> dy;
			x.push_back(make_pair(dx, dy));
		}
		else if (u == "N")
		{
			break;
		}
		else
		{
			cout << "请输入正确的选择" << endl;
		}
	} while (1);
}

vector<double> l_x;      //记录拉格朗系数
double y=0;                //记录最后所求的x对应的值
void L_end(double t, vector<pair<double, double>> x)          //t为需要计算的坐标,x为输入进去的已知离散坐标
{
	int i = 0;
	for (vector<pair<double, double>>::iterator it = x.begin(); it < x.end(); it++)
	{
		l_x.push_back(1);          //拉格朗系数初始化
		int i1 = 0;
		for (vector<pair<double, double>>::iterator it1 = x.begin() ; it1 < x.end();  it1++)
		{
			if (i1 != i)
			{
				double i_first = x[i].first;
				double i1_first = x[i1].first;

				l_x[i] = l_x[i] * ((t - i1_first) / (i_first - i1_first));
			}
			i1++;
		}
		i++;
	}
	int i2 = 0;
	for (vector<double>::iterator it2 = l_x.begin() ; it2 < l_x.end();  it2++)
	{
		double l_x_ = l_x[i2];
		double i2_second = x[i2].second;
		y = y+l_x_ * i2_second;  
		i2++;
	}

	
}

void main()
{
	vector<pair<double, double>> x;
	enter(x);
	cout << "请输入需要计算的x坐标" << endl;
	double temp;
	cin >> temp;
	L_end(temp,x);
	cout << temp << "对应的坐标是:";
	printf("%.5f", y);
}

猜你喜欢

转载自blog.csdn.net/qq_60811855/article/details/126077653