C语言实现的拉格朗日插值算法 /****************************** * Lagrange-Interpolynomials * * copyrihgt by * * su xiaomeng * * zhu zuojia * * (07地信) * ******************************/ #include<stdio.h> #include<math.h> #include<stdlib.h> void main() { int i,j,n; float *x,*y,*L,l1,l2,LN=0,xI,t; printf("How many group numbers (n>1)? /n"); printf(" n="); scanf("%d",&n); x=(float *)malloc(n*sizeof(float)); //动态输入x_1,x_2...x_n的值 printf("Please input the x-value /n x[i]: "); for(i=0;i<n;i++) scanf("%f,",&x[i]); y=(float *)malloc(n*sizeof(float)); //动态输入y_1,y_2....y_n的值 printf("Please input the y-value /n y[i]: "); for(i=0;i<n;i++) scanf("%f,",&y[i]); L=(float *)malloc(n*sizeof(float)); //动态记录插值基函数:l_i(x_j) printf("Plaease input the I-point value /n xI="); //xI是所要求插值点 scanf("%f,",&xI); for(i=0;i<n;i++) { t=x[i]; for(j=0,l1=1,l2=1;j<i;j++) { l1 *=(xI-x[j]);l2 *=(t-x[j]); } for(j=i+1;j<n;j++) { l1*=(xI-x[j]);l2 *=(t-x[j]); } //l_i(x_j) L[i]=l1/l2; //l_i(x_j) } for(i=0;i<n;i++) LN+=L[i]*y[i]; //计算拉格朗日多项式 printf("The result of Lagrange-I is: %f/n",LN); }