Newton多项式插值法

Newton插值法公式:

P n ( x ) = f 0 + ∑ k = 1 n a k ( x − x 0 ) ( x − x 1 ) . . . ( x − x k − 1 ) , a k = f 0 , 1 , 2 , . . . , k , k = n P_{n}(x)=f_{0}+\sum_{k=1}^{n}a_{k}(x-x_{0})(x-x_{1})...(x-x_{k-1}),a_{k}=f_{0,1,2,...,k},k=n Pn(x)=f0+k=1nak(xx0)(xx1)...(xxk1),ak=f0,1,2,...,k,k=n

例:

已知 ( x 0 , f ( x 0 ) ) , ( x 1 , f ( x 1 ) ) , . . . , ( x n , f ( x n ) ) (x_{0},f(x_{0})),(x_{1},f(x_{1})),...,(x_{n},f(x_{n})) (x0,f(x0)),(x1,f(x1)),...,(xn,f(xn))共n+1点的坐标值,如何计算插值函数,关键在于如何建立差商表

#include
int main()
{
	int i,j,n;
	double f[40][40],x[40];
	scanf("%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 %10.5lf",i,x[i]);
		for(j=0;j<=n-i;j++)
		{
			printf("%10.5lf",f[i][j]);
		}
		printf("\n");
	}
	return 0;
}
/*
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

*/

你可能感兴趣的:(算法)