利用online C語言 執行數值分析練習EX1-9.C
/* 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;
}
n=5
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
Divided Difference Table:
=========================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
0 0.00000 -6.00000 1.05170 0.57250 0.21500 0.06302 0.01416
1 0.10000 -5.89483 1.22345 0.70150 0.27802 0.07859
2 0.30000 -5.65014 1.57420 0.95171 0.35661
3 0.60000 -5.17788 2.24040 1.23700
4 1.00000 -4.28172 2.85890
5 1.10000 -3.99583
沒有留言:
張貼留言