Lagrange Interpolation
Source Code:
Write a program in C to find the value of ๐ for ๐ = ๐ given the data using Lagrangeโs Interpolation:
x: 1, 2, 4
y: 1, 4, 16
#include <stdio.h>
#include <conio.h>
int main(){
clrscr();
double x[3]={1,2,4}, y[3]={1,4,16}, xp=3, yp=0, L;
for(int i=0;i<3;i++){
L=1;
for(int j=0;j<3;j++)
if(j!=i) L *= (xp - x[j])/(x[i] - x[j]);
yp += L * y[i];
}
printf("\n\nFor x=%.1f, y=%.2f\n", xp, yp);
getch();
return 0;
}
Output

