拉格朗日线性插值--供大家参考
#include<stdio.h>#define maxsize 100
int main(void)
{
double x[maxsize],y[maxsize],aa,sum,p,a,b;
int i,k,n;
printf("please input a and b:\n");
scanf("%lf %lf",&a,&b);
printf("please input aa=\n");
scanf("%lf",&aa);
printf("please input n=\n");
scanf("%d",&n);
printf("please input x[k]=\n");
for(k=0;k<=n;k++)
{
printf("x[%d]=",k);
scanf("%lf",&x[k]);
}
printf("please input y[k]=\n");
for(k=0;k<=n;k++)
{
printf("y[%d]=",k);
scanf("%lf",&y[k]);
}
if(x[0]<a&&x[n]>b)
{
printf("输入错误,请重新输入!");
return 0;
}
sum=0;
for(k=0;k<=n;k++)
{
p=1;
for(i=0;i<=n;i++)
if(i!=k)
p=p*(aa-x[i])/(x[k]-x[i]);
sum+=p*y[k];
}
printf("sum=%lf\n",sum);
return 0;
}