#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
const int n=4;//插值节点的最大数为四
using namespace std;
double x1[n+1]= {0.4,0.55,0.65,0.8,0.9};
double y1[n+1]= {0.4175,0.57815,0.69657,0.88811,1.02652};
/*
www.quzhuanpan.com
转载请务必告知
*/
void DiffCoefficient(double x1[n+1],double y1[n+1],double F[n+1][n+1])//F[n+1][n+1],记录差商
{
int i,j;
for(i=0; i<=n; i++)
{
F[i][0]=y1[i];//插值为F[0][0]的点
}
for(j=1; j<=n; j++)
for(i=j; i<=n; i++)
{
F[i][j]=(F[i][j-1]-F[i-1][j-1])/(x1[i]-x1[i-j]);//求各阶差商
//printf("%f\n",F[i][j]);
}
printf("%12s%12s","Xi","F(Xi)");
for(j=1; j<=n; j++)
{
printf("%9d%s",j,"step");
}
printf("\n");
for(j=1; j<=38; j++)
{
printf("--");
}
printf("\n");
for(i=0; i<=n; i++)
{
printf("%12f",x1[i]);
for(j=0; j<=i; j++)
{
printf("%13f",F[i][j]);
}
printf("\n");
}
for(i=0; i<=38; i++)
{
printf("--");
}
printf("\n");
}
/*
www.tengxunyun.me
转载请务必告知
*/
int main()
{
int i,m,k;
double P,F[n+1][n+1];
float X;
DiffCoefficient(x1,y1,F);
printf("The number of the interpolation points are m=");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
printf("\ninput X%d=",i);
scanf("%f",&X);
P=F[n][n];
for(k=n-1;k>=0;k--)
{
P=P*(X-x1[k])+F[k][k];
}
printf("P(%f)=%f\n",X,P);
}
getch();
return 0;
}