x f(x)
===========
0 0
1 -3
2 0
3 15
4 48
===========
程式
/* ex1-9.c is for developing the divided-defference
* table for Newton Interpolation polynomial.
*/
#include <stdio.h>
void main()
{
int i,j,n;
double f[40][40],x[40];
scanf("n=%d",&n);
for(i=0;i<=n;i++)
{
scanf("%lf %lf ",&x[i],&f[i][0]);
}
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.1lf ",i,x[i]);
for(j=0;j<=n-i;j++)
{
printf("%8.1lf ",f[i][j]);
}
printf("\n");
}
return;
}
輸入資料
n=4
0.0 0.0
1.0 -3.0
2.0 0.0
3.0 15.0
4.0 48.0
輸出結果
Divided Difference Table:
=================================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
0 0.0 0.0 -3.0 3.0 1.0 0.0
1 1.0 -3.0 3.0 6.0 1.0
2 2.0 0.0 15.0 9.0
3 3.0 15.0 33.0
4 4.0 48.0
f(x)= 0.0 + -3 (x-0.0) + 3 (x-0.0)(x-1.0) + 1.0 (x-0.0)(x-1.0)(x-2.0)
+ 0.0 (x-0.0)(x-1.0)(x-2.0)(x-3.0)
沒有留言:
張貼留言