Friday, August 22, 2014

C Code for Lagrange's Interpolation Formula

#include<stdio.h>
#include<math.h>

int main()
{
  float x[10],y[10], temp, ans[10], sum, point;
  int i,j,k=0,num;

  printf("Enter your points : ");
  scanf("%d",&num);

  for(i=0; i<num; i++)
  {
    printf("x%d is: ", i);
    scanf("%f",&x[i]);
    printf("y%d is: ", i);
    scanf("%f",&y[i]);
  }

  printf("Enter your interpolation number: ");
  scanf("%f",&point);

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

  for(i=0;i<num;i++)
  {
     sum = sum + ans[i];
  }

  printf("f(%.1f) = %f ", point, sum);
  return 0;
}

No comments:

Post a Comment

Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...