青年梦想家 发表于 2015-8-28 16:46

重要的算法C语言实现源代码2

2.牛顿插值多项式,用于离散数据的拟合

C/C++ code
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void difference(float *x,float *y,int n)
{ float *f;
int k,i;
f=(float *)malloc(n*sizeof(float));
for(k=1;k<=n;k++)
{ f=y;
    for(i=0;i<k;i++)
      f=(f-y)/(x-x);
    y=f;
}
return;
}
main()
{ int i,n;
float x,y,xx,yy;
printf("Input n:");
scanf("%d",&n);
if(n>=20) {printf("Error! The value of n must in (0,20)."); getch(); return 1;}
if(n<=0) {printf("Error! The value of n must in (0,20).");getch(); return 1;}
for(i=0;i<=n-1;i++)
{ printf("x[%d]:",i);
    scanf("%f",&x);
}
   printf("\n");
for(i=0;i<=n-1;i++)
{ printf("y[%d]:",i);scanf("%f",&y);}
printf("\n");
difference(x,(float *)y,n);
printf("Input xx:");
scanf("%f",&xx);
yy=y;
for(i=n-1;i>=0;i--) yy=yy*(xx-x)+y;
printf("NewtonInter(%f)=%f",xx,yy);
getch();
}



关于本帖内容欢迎大家踊跃讨论,与在下交流!

页: [1]
查看完整版本: 重要的算法C语言实现源代码2