i x f(x) f ' (x)
=========================================
0 0.0 1.0000 1.0000
1 1.0 2.7183 2.71832 2.0 7.3891 7.3891
=========================================
印出Hermite 差除表
程式
/* ex1-13.c To generate the coeffficients for
* Hermite Interpolating Polynomial H on the distinct numbers
* x0,x1,x2,...with f0,f1,f2,... and f'0,f'1,f'2...
* based on the Newton backward divided-difference Algorithm
* and output the divided_difference table for Hn(x).
*/
#include <stdio.h>
int main()
{
int i,j,k,n;
double x[30],z[30],q[30][30],f[30],ff[30];
printf("input n=");
scanf("n=%d",&n);
for(i=0;i<=n;i++)
{
printf("input x=");
scanf("%lf",&x[i]);
printf("input f=");
scanf("%lf",&f[i]);
printf("input ff=");
scanf("%lf",&ff[i]);
}
printf("i z(i) f(i) f(i-1,i) f(i-2,i-1,i)...\n");
for(i=0;i<=n;i++)
{
z[2*i]=x[i];
z[2*i+1]=x[i];
q[2*i][0]=f[i];
q[2*i+1][0]=f[i];
q[2*i+1][1]=ff[i];
if(i !=0)
q[2*i][1]=(q[2*i][0]-q[2*i-1][0])/(z[2*i]-z[2*i-1]);
if(i==0)
printf("%d %3.1lf %7.4lf\n",i,z[i],q[i][0]);
else if(i==1)
printf("%d %3.1lf %7.4lf %7.4lf\n",i,z[i],q[i][0],q[i][1]);
}
for(i=2;i<=2*n+1;i++)
{
printf("%d %3.1lf %7.4lf %7.4lf",i,z[i],q[i][0],q[i][1]);
for(j=2;j<=i;j++)
{
q[i][j]=(q[i][j-1]-q[i-1][j-1])/(z[i]-z[i-j]);
printf(" %7.4lf",q[i][j]);
}
printf("\n");
}
return 0;
}
輸入資料
n=2
0.0 1.0000 1.0000
1.0 2.7183 2.7183
2.0 7.3891 7.3891
輸出畫面
i z(i) f(i) f(i-1,i) f(i-2,i-1,i)......................................
0 0.0 1.0000
1 0.0 1.0000 1.0000
2 1.0 2.7183 1.7183 0.7183
3 1.0 2.7183 2.7183 1.0000 0.2817
4 2.0 7.3891 4.6708 1.9525 0.4762 0.0973
5 2.0 7.3891 7.3891 2.7183 0.7658 0.1448 0.0238
漢米爾方程式
Hn(x) = 1.0000 + 1.0000 (x-0.0) + 0.7183 (x-0.0)^2 + 0.2817 (x-0.0)^2 (x-1.0)
+ 0.0973 (x-0.0)^2 (x-1.0)^2 + 0.0238 (x-0.0)^2 (x-1.0)^2 (x-2.0)
沒有留言:
張貼留言