Educational Blog: LAGRANGE'S INTERPOLATION Method For Finding X in C

Tuesday, 18 November 2014

LAGRANGE'S INTERPOLATION Method For Finding X in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  float x[10],y[10],temp=1,f[10],sum,p;
  int i,n,j,k=0,c;
  clrscr();
  printf("\nhow many record you will be enter: ");
  scanf("%d",&n);
  for(i=0; i<n; i++)
  {
   printf("\n\nenter the value of x%d: ",i);
   scanf("%f",&x[i]);
   printf("\n\nenter the value of f(x%d): ",i);
   scanf("%f",&y[i]);
  }
  printf("\n\nEnter f(x) for finding x: ");
  scanf("%f",&p);

  for(i=0;i<n;i++)
  {
    temp = 1;
    k = i;
    for(j=0;j<n;j++)
    {
      if(k==j)
      {
        continue;
      }
      else
      {
        temp = temp * ((p-y[j])/(y[k]-y[j]));
      }
    }
    f[i]=x[i]*temp;
  }

  for(i=0;i<n;i++)
  {
     sum = sum + f[i];
  }
  printf("\n\n* * * x = %f * * *",sum);
  getch();
}



 ------------------------OUT PUT-------------------------



how many record you will be enter: 4


enter the value of x0: 20


enter the value of f(x0): 0.342


enter the value of x1: 25


enter the value of f(x1): 0.423


enter the value of x2: 30


enter the value of f(x2): 0.500


enter the value of x3: 35


enter the value of f(x3): 0.650


Enter f(x) for finding x: 0.390


* * * x = 22.840574 * * *

No comments:

Post a Comment