Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Numerical MethodsRSS Feeds

LAGRANGE'S INTERPOLATION METHOD FOR FINDING f(X)

Posted By: Shannon Hughes     Category: C Programming     Views: 47038

Write a program of LAGRANGE'S INTERPOLATION METHOD FOR FINDING f(X).

Code for LAGRANGE'S INTERPOLATION METHOD FOR FINDING f(X) in C Programming

#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 X for finding f(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-x[j])/(x[k]-x[j]));
      }
    }
    f[i]=y[i]*temp;
  }

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


/*
______________________________________

OUT PUT
______________________________________

how many record you will be enter: 4


enter the value of x0: 0


enter the value of f(x0): 0


enter the value of x1: 1


enter the value of f(x1): 2


enter the value of x2: 2


enter the value of f(x2): 8


enter the value of x3: 3


enter the value of f(x3): 27


Enter X for finding f(x): 2.5


f(2.5) = 15.312500


*/
  
Share: 


Didn't find what you were looking for? Find more on LAGRANGE'S INTERPOLATION METHOD FOR FINDING f(X) Or get search suggestion and latest updates.

Shannon Hughes
Shannon Hughes author of LAGRANGE'S INTERPOLATION METHOD FOR FINDING f(X) is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Giannis Kostas from Greece Comment on: Aug 09
Hello,
I am new to applied cryptography and I have some questions concerning the above code and in general Lagrange method and crypto. If the points of the polynomial are selected from a Galois Field (e.g. 2^128) will the Lagrange method be able to estimate the right polynomial? I ask that because in your implementation, you used float numbers instead of integers that GF demands. Also, is there significant difference in using integer arithmetic than finite filed arithmetic? And if there is, when is preferred to use one or the other?
Thank you for your time,
Yiannis

View All Comments