C program to implement the Lagrange interpolation

/* C program to implement the Lagrange interpolation.*/

#include<stdio.h>
#include<conio.h>
#define MaxN 90

void main()
{
float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;
int i, j, n;
clrscr();
printf("Enter the value of n: \n");
scanf("%d", &n);
printf("Enter the values of x and y: \n");
for(i=0; i<=n; i++)
scanf("%f%f", &arr_x[i], &arr_y[i]);
printf("Enter the value of x at which value of y is to be calculated: ");
scanf("%f", &x);
for (i=0; i<=n; i++)
{
  numerator=1;
  denominator=1;
  for (j=0; j<=n; j++)
  if(j!=i)
  {
   numerator *= x-arr_x[j];
   denominator *= arr_x[i]-arr_x[j];
  }
  y+=(numerator/denominator)*arr_y[i];
}
printf("When x=%4.1f y=%7.1f\n",x,y);
getch();
}

 

The out put would look similar to this!!

 

Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...
 
Posts RSSComments RSSBack to top
© 2013 Updated Tech News Results and Reviews