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