已知下列諸點的座標值
xi f(xi)
==============
0.50 0.6915
0.60 0.7257
0.65 0.7422
0.75 0.7734
0.90 0.8159
1.10 0.8643
1.30 0.9032
===============
求牛頓的向前差除表
計算
1). P(0.55) 與 f(0.55)=0.7088 之誤差
2). P(0.70) 與 f(0.70)=0.7580 之誤差
3). P(0.80) 與 f(0.80)=0.7881 之誤差
4). P(1.00) 與 f(1.00)=0.8413 之誤差
===========================
void main()
{
int i,j,n,m;
double f[40][40],x[40],pn,xa[20],dx;
scanf("n=%d m=%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%lf ",&xa[i]);
}
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");
}
printf("\nTHE RESULTS OF INTERPOLATION:\n");
for(j=1;j<=m;j++)
{
dx=1.0;
pn=f[0][0];
for(i=1;i<=n;i++)
{
dx=dx*(xa[j]-x[i-1]);
pn=pn+f[0][i]*dx;
}
printf("Pn(%6.3lf)= %8.5lf\n",xa[j],pn);
}
return;
}
輸入資料
n=6 m=4
0.55 0.7 0.8 1.0
0.50 0.6915
0.60 0.7257
0.65 0.7422
0.75 0.7734
0.90 0.8159
1.10 0.8643
1.30 0.9032
輸出結果
Divided Difference Table:
=========================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
0 0.50000 0.69150 0.34200 -0.08000 -0.16000 0.44444 -0.82540 1.14011
1 0.60000 0.72570 0.33000 -0.12000 0.01778 -0.05079 0.08669
2 0.65000 0.74220 0.31200 -0.11467 -0.00762 0.00989
3 0.75000 0.77340 0.28333 -0.11810 -0.00119
4 0.90000 0.81590 0.24200 -0.11875
5 1.10000 0.86430 0.19450
6 1.30000 0.90320
THE RESULTS OF INTERPOLATION:
Pn( 0.550)= 0.70871
Pn( 0.700)= 0.75810
Pn( 0.800)= 0.78811
Pn( 1.000)= 0.84143
運作畫面
沒有留言:
張貼留言