⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 拉格朗日.cpp

📁 拉格朗日算法
💻 CPP
字号:
//programm of Lagrange 
#include "iostream.h"
#include "iomanip.h"
void main()
{
	int i,j,k;//定义循环变量
	int n;
	double x;
	double y = 0;//初始化为零
	struct point   //使用结构体存储各节点值
	{
		double x1;
		double y1;
	};
	point poi[10];
    cout<<"请输入x的值:"<<endl;
	cin>>x;
	cout<<"请输入要插值的节点数n:"<<endl;
	cin>>n;
	cout<<"请输入各节点(xi,yi):"<<endl;
	for(i=0;i<n;i++)
	{
		cin>>poi[i].x1>>poi[i].y1;
	}
	for(k=0;k<n;k++)
	{
		double t = 1;
		for(j=0;j<n;j++)
		{
			if(j!=k)
				t = ((x - poi[j].x1)*t)/(poi[k].x1 - poi[j].x1);
				else continue;
		}
		
		y = y + t*poi[k].y1;
	}
	cout<<"我们可以得到拉格朗日插值结果为:";
	cout<<y;
	cout<<endl;
}
//一下是我用的一个小例子,下面是运行的过程,例子为求exp(115);
/*请输入x的值:
115
请输入要插值的节点数n:
3
请输入各节点(xi,yi):
100 10 121 11 144 12
我们可以得到拉格朗日插值结果为:10.7228
Press any key to continue*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -