Monday, October 8, 2012

C code - Lagrange's Interpolation


//To do Lagrange's interpolation
//By Mahesha MG, MIT
#include<stdio.h>
main()
{
int i,j,k,n;
float x[10],y[10],l[10],f,val=0;
clrscr();
printf("Enter total number of points (n): ");
scanf("%d",&n);
printf("Enter data points (x <space> y)\n");
            for (i=0;i<n;i++)
       {
            scanf("%f%f",&x[i],&y[i]);
            }
 printf("\nEnter x value: ");
 scanf("%f",&f);
 for(i=0;i<n;i++)
 {
 l[i]=y[i];
 for(j=0;j<n;j++)
 {
 if(i!=j)
  l[i]*=(f-x[j])/(x[i]-x[j]);
  }
  val+=l[i];
  }
 printf("At x = %f, y = %f",f,val);

}

No comments:

Post a Comment