Lagrange Interpolation Polynomial – C PROGRAM

Given a set of n+1 data-points (x_i,y_i) , the Lagrange Interpolating Polynomial is a polynomial of degree \leq n , such that it passes through all the given n+1 data-points.

It is given as,

\boxed{Pn(x) = \sum^n_{i=0} L_i(x)y_i}

where (x_0,y_0), (x_1,y_1),.....(x_n,y_n) are the n+1 data-points.
and,
\boxed{L_i(x)=\prod^n_{j=0;j \neq i}\frac{x-x_j}{x_i-x_j}}

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:

f(x)=1/x

f(x)=x^2

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

[wpedon id="7041" align="center"]

3 thoughts on “Lagrange Interpolation Polynomial – C PROGRAM

  1. 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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.