clear all
xpoints=[0 2 4 7 9 10]
ypoints=[0 5 8 10 2 4]
n=length(xpoints)-1; %order of polynomial
xint=0:0.1:10;
yint=LagrangePolynomial(xpoints,ypoints,n,xint);
hold off
plot(xpoints,ypoints,’ko’,’MarkerSize’,20,’MarkerFaceColor’,’r’)
hold on
plot(xint,yint,’b-‘,’Linewidth’,4)
xlabel(‘x’)
ylabel(‘y’)
function yint=LagrangePolynomial(x,y,n,xint)
yint=zeros(size(xint));
for i=1:n+1
Lix=1.0;
for j=1:n+1
if j ~= i
Lix=Lix.*(xint-x(j))/(x(i)-x(j));
end
end
yint=yint+y(i).*Lix;
end
end