Professional Documents
Culture Documents
Lagrange' Interpolation
Lagrange' Interpolation
THEORY:
1. Start
2. Read number of data (n)
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
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:
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.