Given a set of data-points , the Lagrange Interpolating Polynomial is a polynomial of degree , such that it passes through all the given data-points.

It is given as,

where are the data-points.

and,

### CODE:

/********************************** ******LAGRANGE INTERPOLATION******* **********************************/ #include<stdio.h> /*Function to evaluate Li(x)*/ double Li(int i, int n, double x[n+1], double X){ int j; double prod=1; for(j=0;j<=n;j++){ if(j!=i) prod=prod*(X-x[j])/(x[i]-x[j]); } return prod; } /*Function to evaluate Pn(x) where Pn is the Lagrange interpolating polynomial of degree n*/ double Pn(int n, double x[n+1], double y[n+1], double X){ double sum=0; int i; for(i=0;i<=n;i++){ sum=sum+Li(i,n,x,X)*y[i]; } return sum; } main(){ int i,n; //n is the degree printf("Enter the number of data-points:\n"); scanf("%d",&n); //no. of data-points is n+1 n=n-1; //Arrays to store the (n+1) x and y data-points of size n+1 double x[n+1]; double y[n+1]; printf("Enter the x data-points:\n"); for(i=0;i<n+1;i++){ scanf("%lf",&x[i]); } printf("Enter the y data-points:\n"); for(i=0;i<n+1;i++){ scanf("%lf",&y[i]); } double X; //value of x for which interpolated value is required printf("Enter the value of x for which you want the interpolated value of y(x):\n"); scanf("%lf",&X); printf("The interpolated value is %lf",Pn(n,x,y,X)); }

### OUTPUT:

### YouTube Tutorial:

### Android App:

https://play.google.com/store/apps/details?id=com.bragitoff.lagrangeinterpolatingpolynomial

### References and Resources:

http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html

https://en.wikipedia.org/wiki/Lagrange_polynomial

http://wmueller.com/precalculus/families/lagrange.html

I’m a physicist specializing in computational material science with a PhD in Physics from Friedrich-Schiller University Jena, Germany. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT, and Machine Learning related apps and software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.

Hello just check the function LI(……) , as per your code i was getting an error while using double x[n+1] , i tried and changed each parameter x[n+1] with x[] and same with y[n+1] with y[] , just check the below code 🙂

8:33: error: use of parameter outside function body before ‘]’ token

8:34: error: expected ‘)’ before ‘,’ token

8:35: error: expected unqualified-id before ‘double’

solution:

/**********************************

******LAGRANGE INTERPOLATION*******

**********************************/

#include

/*Function to evaluate Li(x)*/

double Li(int i,int n,double x[],double X){

int j;

double prod=1;

for(j=0;j

Thank you very much for your valuable correction to the above program. I can successfully run a program as per your correction suggestion.

how can i find a weight of interval [0, 1] and n is 5 and h = 1/n?