DEV c++ 利用C Code 建立牛頓差除表
/* ex1-9.c is for developing the divided-defference
* table for Newton Interpolation polynomial.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i,j,n;
double f[40][40],x[40];
printf("\n Please input n: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("\n Please input x[i]: ");
scanf("%lf",&x[i]);
printf("\n Please input f[i]: ");
scanf("%lf",&f[i][0]);
printf("%lf %lf ",x[i],f[i]);
}
printf("\n Divided Difference Table:\n");
printf(" =========================\n");
for(j=1;j<=n;j++)
{
for(i=0;i<=n-j;i++)
f[i][j]=(f[i+1][j-1]-f[i][j-1])/(x[i+j]-x[i]);
}
printf("i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......\n");
for(i=0;i<=n;i++)
{
printf("%d %8.5lf ",i,x[i]);
for(j=0;j<=n-i;j++)
printf("%8.5lf ",f[i][j]);
printf("\n");
}
//=====================================================
printf("enter any key to exit");
fflush(stdin);
getchar();
//=====================================================
return 0;
}
沒有留言:
張貼留言