Tuesday, July 2, 2013

C PROGRAM FOR LAGRANGE'S INTERPOLATION FORMULA

/*PROGRAM FOR LAGRANGE'S INTERPOLATION FORMULA*/

#include<stdio.h>
#include<conio.h>
void main () {
  int i,j, n;
  float x[20],y[20], p[20],f,k=0;
  clrscr();
  printf("Enter the number of terms:");
  scanf("%d",&n);
  printf("Enter the values of:\nx y\n");
  for(i=0;i<n;i++) {
  scanf("%f %f",&x[i], &y[i]);
  }
  printf("Enter value of x for which y is to be found:");
  scanf("%f",&f);
  for(j=1;j<=n;j++) {
  p[j]=1.0;
  for(i=1;i<=n;i++) {
  if(i==j)
  continue;
  p[j]=p[j] * (f-x[i])/(x[j]-x[i]);
  }
  }
  for(i=1;i<=n;i++)
  k = k + p[i]* y[i];
  printf("for x = %f, y = %f",f,k);

getch();
}

/*OUTPUT:-
Enter the number of terms:4
Enter the values of:
x       y
5       12
6       13
9       14
11      16
Enter value of x for which y is to be found:10
for x = 10.000000, y = 14.744108   */

No comments:

Post a Comment