You are on page 1of 3

LAGRANGE’S INTERPOLATION METHOD:

THEORY:

Lagrange Interpolation is a method in numerical analysis to determine a polynomial that fits


a set of data points. It finds the polynomial that passes through each given data point,
producing a smooth curve that approximates the data. This polynomial is constructed using
Lagrange polynomials, which are based on the idea of forming a linear combination of
simple polynomials. The resulting polynomial is called the Lagrange Interpolating
Polynomial.

If y = f(x) takes the value of y0 , y1 , y2 , y3 , ... , yn corresponding to x0 , x1 , x2 , x3 , ... ,


xn then

ALGORITHM FOR LAGRANGE’S INTERPOLATION METHOD:

1. Start
2. Read number of data (n)

3. Read data Xi and Yi for i=1 ton n

4. Read value of independent variables say xp


whose corresponding value of dependent say yp is to be determined.

5. Initialize: yp = 0

6. For i = 1 to n
Set p = 1
For j =1 to n
If i ≠ j then
Calculate p = p * (xp - Xj)/(Xi - Xj)
End If
Next j
Calculate yp = yp + p * Yi
Next i

6. Display value of yp as interpolated value.

7. Stop
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float x[20], f[20], L[20], sum = 0, X;
int N;
int i,j;
printf("Enter N:\n");
scanf("%d",&N);
// TAKING INPUT
printf("Enter x:\n");
for(i = 0; i<N; i++)
{
printf("x%d: ",i);
scanf("%f",&x[i]);
}
printf("\nEnter F:\n");
for(i = 0; i<N; i++)
{
printf("f%d: ",i);
scanf("%f",&f[i]);
}
// CALCULATION
printf("Put X = ");
scanf("%f",&X);
for(i = 0; i<N; i++)
{
L[i] = 1;
for(j = 0; j<N; j++)
{
if(i!=j)
{
L[i] = L[i]*((X-x[j])/(x[i]-x[j]));
}
}
sum += f[i]*L[i];
}
printf("\nPn(%.0f) = %.4f",X,sum);
return 0;
}
OUTPUT:

DISCUSSION AND CONCLUSION:

Here, we discussed about the algorithm and source code of Lagrange’s interpolation method
and we practically coded the source code of the Lagrange’s interpolation method using
programming language ( C ) to test the example of solution of a problem relating Lagrange’s
interpolation method.

You might also like