You are on page 1of 1

Program Name: Lagranges Interpolation

clc;
clear;
n=input('\nEnter the number of elements: ');
xg=input('\nxg= ');
for(i=1:n)
x(i)=input('\nEnter x= ');
y(i)=input('\nEnter y= ');
end
yg=0;
for(j=1:n)
l=1;
for(i=1:n)
if(j~=i)
l=l*(xg-x(i))/(x(j)-x(i));
end
end
yg=yg+l*y(j);
end
fprintf('\nyg= %f',yg);
OUTPUT:Enter the number of elements: 4
xg= 1.5
Enter x= 0
Enter y= 2
Enter x= 1
Enter y= 3
Enter x= 2
Enter y= 12
Enter x= 5
Enter y= 147
Value of yg= 6.125000>>

You might also like