lagrange.cpp

来自「这事lanrange插值算法实例」· C++ 代码 · 共 35 行

CPP
35
字号
#include <iostream.h>
#include <math.h>
#include <iomanip.h>

double Lagrange(double x[],double y[],int n,double xx)
{
	double yy=0.0;

	for(int k=0;k<n;k++)
	{
		double temp=1.0;
		for(int j=0;j<n;j++)
		{
			if(j==k)
				continue;
			temp*=(xx-x[j])/(x[k]-x[j]);
		}
		yy+=temp*y[k];
	}

	return yy;
}

void main()
{
	double x=115;
/*	double y=Simpson(100,10,121,11,144,12,x);
	cout<<"the square root of the "<<x<<": "<<y<<endl;*/

	//x=115;
	double x0[3]={100,121,144};
	double y0[3]={10,11,12};
	double y=Lagrange(x0,y0,3,x);
	cout<<"the square root of the "<<x<<": "<<y<<endl;
}

⌨️ 快捷键说明

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