Lagrange Interpolation in C and C++

Interpolation is a method of finding new data points within the range of a discrete set of known data points. In simpler words, Interpolation is the means to find the value of a mathematical function at any data point, provided a data set of x and f(x) values are provided to estimate the function. One of the many ways to find interpolation at a certain arbitrary point is Lagrange’s Interpolation. Lagrange’s Interpolation is a method used to find the value of a function at any arbitrary point. A data set of discrete values of x and f(x) are given and using these values, a function is found which satisfies all these values of x and gives the correct value of y. This equation can then be used to find the value of f(x) at any arbitrary value x. The formula used for Lagrange’s Interpolation is

lagrange interpolation

Lagrange Interpolation in C Program

/* ******************************
Author: Exploring Bits
****************************** */
#include <stdio.h>
#include <conio.h>
void lagrangeInterpolation(double x[], double fx[], int n, double xp){
double intp = 0, m;
int i, j;
for (i = 0; i < n; i++){
m = 1;
for (j = 0; j < n; j++){
if (i != j)
m = m * (xp – x[j]) / (x[i] – x[j]);
}
m = m * fx[i];
intp = intp + m;
}
printf(“\nValue of f(%lf) is %lf\n”, xp, intp);
}
int main(){
int n, i;
double x[100], fx[100], xp;
printf(“Enter the number of points in the data: “);
scanf(“%d”, &n);
printf(“Enter the data: \n”);
for (i = 0; i < n; i++){
printf(“x%d: “, (i+1));
scanf(“%lf”, &x[i]);
printf(“f(x%d): “, (i+1));
scanf(“%lf”, &fx[i]);
}
printf(“Enter the Interpolation point: “);
scanf(“%lf”, &xp);
lagrangeInterpolation(x, fx, n, xp);
return 0;
}

 

Lagrange Interpolation in C ++ Program

/* ******************************
Author: Exploring Bits
****************************** */
#include <iostream>
using namespace std;
void lagrangeInterpolation(double x[], double fx[], int n, double xp){
double intp = 0;
for (int i = 0; i < n; i++){
double m = 1;
for (int j = 0; j < n; j++){
if (i != j)
m = m * (xp – x[j]) / (x[i] – x[j]);
}
m = m * fx[i];
intp = intp + m;
}
cout << “\nValue of f(“ << xp << “) is “ << intp << endl;
}
int main(){
int n;
cout << “Enter the number of points in the data: “;
cin >> n;
double x[n], fx[n];
cout << “Enter the data: “ << endl;
for (int i = 0; i < n; i++){
cout << “x” << (i+1) << “: “;
cin >> x[i];
cout << “f(x” << (i+1) << “): “;
cin >> fx[i];
}
double xp;
cout << “Enter the Interpolation point: “;
cin >> xp;
lagrangeInterpolation(x, fx, n, xp);
return 0;
}

 

Output of Lagrange Interpolation in C and C++

In the above program, the main () function is responsible for inputting the values of x and f(x) from the user into two different arrays. Then it takes the interpolation point from the user, this is the value at which the value of the function is to be calculated. The lagrangeInterpolation () function takes the two data arrays as arguments and then creates an equation and solves it to find the value of f(x) at the interpolation point using the formula given above.

Lagrange’s Interpolation has a disadvantage as well. If one of the values of x in the dataset is changed, then f(x) also has to be changed. In such a scenario, all the values and the Lagrange’s polynomial has to be recalculated all over again. This means that Lagrange’s Interpolation is not flexible to changes in the data points.

5 thoughts on “Lagrange Interpolation in C and C++”

Leave a Comment