Skip to main content

C program for Lagrange Interpolation

Lagrange Interpolation Example

#include<stdio.h>
#include<conio.h>

void main()
{
  float ax[100],ay[100],x,y=0,temp;
  int n,i,j;
 
  printf("Enter n: ");
  scanf("%d",&n);

  printf("Enter given data...");
  for(i=0;i<=n;i++)
  {
    printf("\nEnter x[%d] and y[%d]: ",i,i);
    scanf("%f%f",&ax[i],&ay[i]);
  }

  printf("Enter x: ");
  scanf("%f",&x);

  for(i=0;i<=n;i++)
  {
    temp=1;
    for(j=0;j<=n;j++)
    {
      if(j!=i)
      {
        temp*=(x-ax[j])/(ax[i]-ax[j]);
      }
    }
    y+=temp*ay[i];
  }
  printf("Hence, f(%f) = %f",x,y);
  getch();
}

Comments

Popular posts from this blog