Lagrange’s Interpolation

Lagrange’s Interpolation

#include<stdio.h>
#include<conio.h>
int main ()
{
	int n,i,j;
   float x[10],f[10],fp,lf=1.0,sum=0.0,xp;
   printf("input number of data points[n]: ");
   scanf ("%d",&n);
   printf("Input data points x(i) and value f(i) [one set in each line]: ");
   for(i=1;i<=n;i++)
   	scanf("%f%f",&x[i],&f[i]);
   printf("\nInput X at which interpolation is required: ");
   scanf("%f",&xp);
   for(i=1;i<=n;i++)
   {
      lf=1.0;
   	for (j=1;j<=n;j++)
      {
      	if(i!=j)
         	lf=lf*(xp-x[j])/(x[i]-x[j]);
      }
      sum=sum+lf*f[i];
   }
   fp=sum;
   printf("Lagragian interpolation at x=%f is %f ",xp,fp);
   getch();
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *