Thursday, January 11, 2018

Lagrange's Interpolation Method Program

#include <stdio.h>
#include <stdlib.h>
int main(){
   int n,i,j;
   float x,l,summation = 0;
   float ax[50], fx[50],Lx[50];
   clrscr();
   printf("Enter number of points : ");
   scanf("%d", &n);
   printf("Enter the value of x : ");
   scanf("%f",&x);
   for(i=0;i<n;i++){
       printf("Enter the value of x and fx at i=%d\n", i);
       scanf("%f%f", &ax[i], &fx[i]);
   }

   for(i=0;i<n;i++){
       l = 1.0f;
       for(j=0;j<n;j++){
           if(j != i){
               l=l*((x-ax[j])/(ax[i]-ax[j]));
           }
       }
       Lx[i]=l;
   }
   for(i=0; i <n ; i++){
       summation=summation+fx[i]*Lx[i];
   }
   printf("Interpolation value is %0.4f ", summation);
   getch();
   return 0;

}

No comments:

Post a Comment