C Program For Lagrange’s Interpolation Method

Lagrange’s interpolation is also an nth degree polynomial approximation to f(x).

In numerical analysis, Lagrange polynomials are used for polynomial interpolation. For a given set of points (xj,yj) with no two xj values equal, the Lagrange polynomial is the polynomial of the lowest degree that assumes at each value xj the corresponding value yj.

Although named after Joseph-Louis Lagrange, who published it in 1795, the method was first discovered in 1779 by Edward Waring.

Lagrange’s Interpolation Method Formula

Lagrange's interpolation method formula

Lagrange’s Interpolation Method Example

Find Solution using Lagrange’s Interpolation formula

xf(x)
3002.4771
3042.4829
3052.4843
3072.4871

x = 301

Solution:
The value of the table for x and y

x300304305307
y2.47712.48292.48432.4871

Lagrange’s Interpolating Polynomial
The value of x at you want to find Pn(x):x=301

Lagrange’s formula is

Lagrange's Interpolation Method Solved Example

C Program

//Lagrange's Interpolation Method
//This code is written by Souvik Ghosh
#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;
}
void 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 %0.4f",Pn(n,x,y,X));
}



Output Terminal

Lagrange's Interpolation Method Output

Related Posts

Artificial Intelligence Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Mixed Signal Design Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Fiber Optic Communication Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Cyber Security Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.

MAKAUT 7th Semester Examination Questions – 2023

Mobile Communication and Networks (PE-EC701C) Neural Network and Fuzzy Logic Control (PE-EC702C/PEROB701B) Principles of Management (HS-HU701)

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

Leave a Reply

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