计算方法-牛顿插值多项式--实现代码
#include<stdio.h>#define maxsize 100
int main(void)
{
double x[maxsize],y[maxsize],m,sum,p;
int i,k,n;
printf("please input m=\n");
scanf("%lf",&m);
printf("please input n=\n");
scanf("%d",&n);
printf("please input x[i]=\n");
for(i=0;i<=n;i++)
{
printf("x[%d]=",i);
scanf("%lf",&x[i]);
}
printf("please input y[i]=\n");
for(i=0;i<=n;i++)
{
printf("y[%d]=",i);
scanf("%lf",&y[i]);
}
for(k=1;k<=n;k++)
for(i=n;i>=k;i--)
y[i]=(y[i]-y[i-1])/(x[i]-x[i-k]);
sum=y[0];
for(i=1;i<=n;i++)
{
p=1;
for(k=0;k<=i-1;k++)
p=p*(m-x[k]);
sum+=p*y[i];
/*sum=y0+y1*(x-x0)+y2*(x-x0)*(x-x1)+y3*(x-x0)*(x-x1)*(x-x2)*/
}
printf("sum=%lf\n",sum);
return 0;
}