Newton Interpolation

王朝other·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

/* 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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航