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

📄 lagrange.cpp

📁 拉格朗日插值算法
💻 CPP
字号:
// lagrange.cpp: implementation of the lagrange class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "lagranger.h"
#include "lagrange.h"
#include "math.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

lagrange::lagrange()
{

}

lagrange::~lagrange()
{

}

double set_x_y(int n);
double cul_average(double x,double y);

void main()
{
//	double x[]={0*PI,PI/6,PI/4,PI/3,PI/2};
//	double y[]={0,0.5,0.707106,0.886025,1};

//	double x[]={0.32,0.34,0.36};//  x
//	double y[]={0.314567,0.333487,0.352274};//y=sin(x)

//	double x[]={1,1.05,1.07};
//	double y[]={2.71828,3.28630,3.52761};
	double X;
	const int n=3;
    double h[n][n],A[n],a[n][n],b[n],c[n],B[n],Y[n],x[n],y[n];
	int i,j;

	for(i=0;i<n;i++)
	{
		cout<<"x["<<i<<"]=";
		double temp;
		cin>>temp;
		x[i]=temp;
		if(!cin)
		{
			cin.clear();
			cout<<endl<<"bad input"<<endl;
			if(cin.get()!='\n')
				continue;
			else if(temp=='q')
				break;
		}
		
		cout<<"y["<<i<<"]=";
		cin>>temp;
		y[i]=temp;
		
	}

	cout<<"please input X"<<endl;
	cin>>X;

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			h[i][j]=x[i]-x[j];
			h[j][j]=1;	
		}
		
	}

	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
            a[i][0]=h[i][0];
            a[i][j]=a[i][j-1]*h[i][j];
		}
		A[i]=a[i][n-1];
		b[i]=X-x[i];	
		c[0]=b[0];
		c[i]=c[i-1]*b[i];
	}

	for(i=0;i<n;i++)
	{
		
		B[i]=c[n-1]/b[i];	
		Y[0]=(B[0]/A[0])*y[0];
		Y[i]=Y[i-1]+(B[i]/A[i])*y[i];
	}
	cout<<Y[n-1]<<endl;

/*
	for(int i=0;;i++)
	{ 
		double x,y,z;
        x=set_x_y(0);
		if(x==0)
		    break;
       	y=set_x_y(1);
		if(y==0)
			break;
	    z=cul_average(x,y);
	    cout<<z<<endl;
	
	}
}

double set_x_y(int n)
{
	if(n==0)
		cout<<"please input x"<<endl;
	else if(n=1)
		cout<<"please intput y"<<endl;
	double temp;
	cin>>temp;
	return temp;
}


double cul_average(double x,double y)
{
	double temp;
	temp=2.0*x*y/(x+y);
	return temp;*/
}

⌨️ 快捷键说明

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