Lagrange Interpolation

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

/* Lagrange.c

Project Name: Lagrange Interpolation

Author: Xunkan

Version: 1.01

History: Apr 2, 2005

Fix a bug in FuncLi

Mar 31, 2005

Build the first edition

Note: For Song Shasha only~!

*/

#include <stdio.h>

int k;

double iX;

double x[100],y[100];

int cx,cy; //Counter for x y array

double FuncLi(double iix,int i);

double FuncPn(double iix);

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("Input Y array -- End with a number less than -9999\n");

k=0;

do {

scanf("%le", &y[k]);

k=k+1;

} while((k<100)&&(y[k-1]>-10000));

if (y[k-1]<=-10000)

{

cy=k-1;

}

else

{

cy=k;

}

printf("You entered %d valid elements of Y array.\n\n",cx);

printf("Answer is %e\n\n",FuncPn(iX));

printf("Press enter key to quit!\n");

getchar();

getchar();

return 0;

}

double FuncLi(double iix,int i)

{

int j;

double rtValue;

rtValue=1;

for (j=0;j<cx;j=j+1)

{

if (j!=i)

rtValue=rtValue+(iix-x[j])/(x[i]-x[j]);

}

return rtValue;

}

double FuncPn(double iix)

{

int i;

double rtValue;

rtValue=0;

for (i=0;i<cy;i=i+1)

{

rtValue=rtValue+(y[i])*FuncLi(iix,i);

}

return rtValue;

}

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