已知六點的座標值 使用牛頓內插法求 f(0.2)之值 ?
n=6
x f(x)
=============
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
=============
利用 https://www.onlinegdb.com/online_c_compiler
#include<stdio.h>
main()
{
int i,j,n,k;
float X[10],Y[10],d[10][10],x,t,y=0,s=1;
printf("\nNewton's Divided Interpolation : \n");
printf("\nEnter the value of n : ");
scanf("%d",&n);
printf("\nEnter the value of X[i] and Y[i] : \n");
printf("\nX[i]\tY[i]\n");
for(i=0;i<n;i++)
scanf("%f%f",&X[i],&Y[i]);
printf("\nEnter the value of x : ");
scanf("%f",&x);
for(j=0;j<n;j++)
for(i=0;i<n-j;i++)
d[i][j]=0;
for(i=0;i<n;i++)
d[i][0]=Y[i];
for(j=0;j<n;j++)
for(i=0;i<n-j;i++)
{
if(j==0) continue;
d[i][j]=(d[i+1][j-1]-d[i][j-1])/(X[i+j]-X[i]);
}
y=Y[0];
for(k=1;k<n;k++)
{
s=s*(x-X[k-1]);
t=s*d[0][k];
y=y+t;
}
printf("\nAnswer = %f\n\n",y);
}
輸入資料
6
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
0.2
輸出結果
Newton's Divided Interpolation :
Enter the value of n :
Enter the value of X[i] and Y[i] :
X[i] Y[i]
Enter the value of x :
Answer = -5.778599
沒有留言:
張貼留言