C Program to implement Lagrange interpolation method to interpolate value of y for the given value of x

/*Program to implement Lagrange interpolation method to interpolate
value of y for the given value of x
*/
#include<stdio.h>
#include<conio.h>
void main()
{

int i,j,n;
float product,sum,x[10],y[10],a;
clrscr();
printf("\n Enter The no of data points ");
scanf("%d",&n);
printf("\nEnter the data points (x,y) ");
for(i=0;i<n;i++)
{
printf("\nEnter x[%d]: ",i);
scanf("%f",&x[i]);
printf("\nEnter y[%d]: ",i);
scanf("%f",&y[i]);
}
printf("\n Enter the interpolated value ");
scanf("%f",&a);
sum=0.0;
for(i=0;i<n;i++)
{
product=1.0;
for(j=0;j<n;j++)
{
if(j!=i)
product=product*((a-x[j])/(x[i]-x[j]));
}
sum=sum+product*y[i];
}

printf("\n The value of Y at given x = %d is : %f",a,sum);
getch();
}

Comments

Popular Posts