You are on page 1of 1

Lagranges interpolation

clc;
clear all;
close all;
n=input('enter the number of points=');
x=input('enter the number of x coordinates=');
y=input('enter the number of y coordinates=');
xg=input('enter the value of xg=');
yg=0;
for i=1:n
L(i)=1;
for j=1:n
if i~=j
L(i)=(L(i)*xg-x(j))/(x(i)-x(j));
end
end
yg=yg+L(i)*y(i);
end
fprintf('the value of xg=%f for yg=%f',xg,yg);

output

enter the number of points=5

enter the number of x coordinates=[1 1.3 1.6 1.9 2.2]

enter the number of y coordinates=[0.1411 -0.6878 -0.9962 -0.5507 0.3115]

enter the value of xg=1.5

the value of xg=1.500000 for yg=-0.9744>>

solver

yg=interp1(x,y,1.5,'spline')

yg =

-0.9744

You might also like