LAGRANGE INTERPOLATION C-SOURCE CODE
\* define library functions before going to function main */
void main()
{
int n,i,j,c;
float f[10],x[10],y[10],t=1,sum=0,p,s=1;
clrscr();
printf("enter the number of data pairs\t");
scanf("%d",&n);
/* Read Data pairs from table */
for(i=0;i<n;i++)
{
printf("\n enter the value of x[%d]:\t",i);
scanf("%f",&x[i]);
printf("\n enter the value of f[x[%d]:\t",i);
scanf("%f", &y[i]);
}
printf("\n enter x for which you have to find f(x):\t");
scanf("%f",&p);
/* execution of lagrange polynomial at the entered value p */
for(i=0;i<n;i++)
{
t=1;
s=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
t=t*(p-x[j]);
s=s*(x[i]-x[j]);
}
}
sum=sum+(t/s)*y[i];
}
/*Print the value of f(x) at p*/
printf("\n value of f at p=%f", sum);
getch();
}
\* define library functions before going to function main */
void main()
{
int n,i,j,c;
float f[10],x[10],y[10],t=1,sum=0,p,s=1;
clrscr();
printf("enter the number of data pairs\t");
scanf("%d",&n);
/* Read Data pairs from table */
for(i=0;i<n;i++)
{
printf("\n enter the value of x[%d]:\t",i);
scanf("%f",&x[i]);
printf("\n enter the value of f[x[%d]:\t",i);
scanf("%f", &y[i]);
}
printf("\n enter x for which you have to find f(x):\t");
scanf("%f",&p);
/* execution of lagrange polynomial at the entered value p */
for(i=0;i<n;i++)
{
t=1;
s=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
t=t*(p-x[j]);
s=s*(x[i]-x[j]);
}
}
sum=sum+(t/s)*y[i];
}
/*Print the value of f(x) at p*/
printf("\n value of f at p=%f", sum);
getch();
}
No comments:
Post a Comment