/* Newton.c
Project Name: Newton Interpolation
Author: Xunkan
Version: 1.00
History: Mar 31, 2005
Build the first edition
Note: For Song Shasha only~!
*/
#include <stdio.h>
int k;
double iX;
double x[100];
int cx; //Counter for x y array
double fTable[100];
int fTableStatus[100];
double FuncWn(int iMaxN);
double FuncF(int iMaxK);
double FuncNn();
int main(int argc, char* argv[])
{
printf("Input x:\n");
scanf("%le", &iX);
printf("Input X array -- End with a number less than -9999\n");
k=0;
do {
scanf("%le", &x[k]);
k=k+1;
} while((k<100)&&(x[k-1]>-10000));
if (x[k-1]<=-10000)
{
cx=k-1;
}
else
{
cx=k;
}
printf("You entered %d valid elements of X array.\n\n",cx);
printf("Answer is %e\n\n",FuncNn());
printf("Press enter key to quit!\n");
getchar();
getchar();
return 0;
}
double FuncWn(int iMaxN)
{
int n;
double rtValue;
rtValue=1;
for(n=0;n<=iMaxN;n++)
{
rtValue=rtValue*(iX-x[n]);
}
return rtValue;
}
double FuncF(int iMaxK)
{
int n;
double rtValue;
if (fTableStatus[iMaxK]==1)
{
return fTable[iMaxK];
}
else
{
//再附加判断一下当前iMaxK是否是0或1,针对这两个单独计算一下
rtValue=(FuncF(iMaxK-2)-FuncF(iMaxK-1))/(x[iMaxK]-x[iMaxK-1]);
fTable[iMaxK]=rtValue;
fTableStatus[iMaxK]=1;
}
return rtValue;
}
double FuncNn()
{
int n;
double rtValue;
rtValue=0;
for(n=0;n<cx;n++)
{
rtValue=rtValue+FuncF(n)*FuncWn(n-1);
}
return rtValue;
}