C語言 例題3-3不等距的微分近似解
中計算f 0,1 , f0,1,2 f0,1,2,3 .....
輸入資料
n=3
0.5 0.4794
0.6 0.5646
0.8 0.7174
1.05 0.8674
程式
/* 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.5lf ",i,x[i]);
for(j=0;j<=n-i;j++)
{
printf("%8.5lf ",f[i][j]);
}
printf("\n");
}
return;
}
輸出畫面
Divided Difference Table:
=========================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2) f(i,i+1.i+2,i+3),......
0 0.50000 0.47940 0.85200 -0.29333 -0.12929
1 0.60000 0.56460 0.76400 -0.36444
2 0.80000 0.71740 0.60000
3 1.05000 0.86740
f0,1= 0.85200
f0,1,2=-0.29333
f0,1,2,3=-0.12929
沒有留言:
張貼留言