You are on page 1of 1

%Polinomio de Lagrange

function [yi,pol]=lagrange2(xs,ys,x)
n=length(xs);
if length (ys)~=n, error('x e y deben ser de la misma longitud');
end
yi=0; pol='0';
for i=1:n
producto=ys(i);
termino=num2str(ys(i));
for j=1:n
if i~=j
producto=producto*(x-xs(j))/(xs(i)-xs(j));
termino=strcat(termino,'*(x-',num2str(xs(j)),')/(',num2str(xs(i)),
'-',num2str(xs(j)),')');

end

end
yi=yi+producto;
pol=strcat(pol,'+',termino);
end

You might also like