用拉格朗日插值多项式求函数的近似值
1 //用拉格朗日插值多项式求函数的近似值 2 #include<iostream> 3 using namespace std; 4 void main() 5 { 6 int n,i; 7 float xx,yy; 8 float *x,*y; 9 cout<<"请输入数据表的数据组数:"; 10 cin>>n; 11 x=new float[n]; 12 y=new float[n]; 13 14 cout<<"请依次输入X的值:"; 15 for(i=0;i<n;i++) 16 cin>>*(x+i); 17 18 cout<<"请依次输入y的值:"; 19 for(i=0;i<n;i++) 20 cin>>*(y+i); 21 22 cout<<"请输入所要计算的X的值:"; 23 cin>>xx; 24 25 float Lagrange(float*,float*,float xx, int n); 26 yy=Lagrange(x,y,xx,n); 27 cout<<"当x="<<xx<<"时,"<<"y="<<yy<<endl; 28 } 29 30 float Lagrange(float*x,float*y,float xx,int n) 31 { 32 int i,j; 33 float *a,yy=0; 34 a=new float[n]; 35 for(i=0;i<=n-1;i++) 36 { 37 *(a+i)=*(y+i); 38 for(j=0;j<=n;j++) 39 if(j!=i) *(a+i)*=(xx-x[j])/(x[i]-x[j]); 40 yy+=*(a+i); 41 } 42 delete a; 43 return yy; 44 }