Saturday, 8 March 2014

LAGRANGE INTERPOLATION PROGRAM

/*CSEMATTER.BLOGSPOT.IN
Program to implement lagrange interpolation*/

#include<stdio.h>
#include<conio.h>
int main()
{int x,i,j,N,D,n;
int X[20],y[20];
float sum=0.0000;
printf("Enter the no. of terms ");
scanf("%d",&n);
printf("Enter the values of x ");
scanf("%d",&X[20]);
printf("Enter the values of y ");
scanf("%d",&y[20]);
printf("Enter the value of x for which value of y is to be found ");
for(i=0;i<n;i++)
{N=D=1;
for(j=0;j<n;j++)
   if(i!=j)
 {N=N*(x-X[j]);
      D=D*(X[i]-X[j]);
}
     sum=sum+(N/D)*y[i];
}
printf("Sum=%f",sum);
getch();
}


Output:
Enter the no. of terms 5
Enter values of x 5 7 11 13 17
Enter the values of y 150 392 1452 2366 5202
Enter the value of x for which the value of y is to be found 9
Sum=810.000000 

No comments:

Post a Comment