# 拉格朗日二元N点插值(C++版)

/////拉格朗日二元N点插值
/////////////////
#include
using namespace std;
//////////
//////////
double LAG2N(int n,int m,double *x,double *tx1,double *tx2,double *ty);
//////////
int main()
{
int i,j,n,m;
double x[2],y;
cout<>n;
cout<<"m=";cin>>m;
while(n*m<4)
{
cout< cout<<"请重新输入二元拉格朗日插值的数据对个数: n=";cin>>n;
cout<<"m=";cin>>m;
}
double *tx1=new double[n];
double *tx2=new double[m];
double *ty=new double[m];
cout< for(i=0;i for(j=0;j {
cout<<"数据点:x1["<>tx1[i];
cout<<"x2["<>tx2[j];
cout<<"对应的函数值y=";cin>>ty[i];
cout< }
cout<<"请输入初始点值: x0(1)=";cin>>x[0];
cout<<"x0(2)=";cin>>x[1];
cout< y=LAG2N(n,m,x,tx1,tx2,ty);
cout<<"x=("< delete [] tx1;
delete [] tx2;
delete [] ty;
return 0;
}
//////////
double LAG2N(int n,int m,double *x,double *tx1,double *tx2,double *ty)
{
double y=0.0;
int i,j;
double *lx1=new double[n];
double *lx2=new double[m];
for(i=0;i {
for(j=0;j {
if(j!=i)
lx1[i]=(x[0]-tx1[j])/(tx1[i]-tx1[j]);
}
}
for(i=0;i {
for(j=0;j {
if(j!=i)
lx2[i]=(x[1]-tx2[j])/(tx2[i]-tx2[j]);
}
}
for(i=0;i for(j=0;j {
y+=lx1[i]*lx2[j]*ty[i*m+j];
}
delete [] lx1;
delete [] lx2;
return (y);
}
//////////

• 博文量
17
• 访问量
335046