I have to write code in matlab for a lagrangian interpolating polynomial that intakes some set of x and y values and a desired x value (named 'c' here) to be estimated and outputs the interpolating polynomial's value at 'c' in matlab
here is the code that I wrote:
function polyatc = lagrange(Xlist, Ylist, c)
polyatc = 0;
xprod=1;
for i=1:size(Ylist)
for j= setdiff(1:size(Xlist), i)
xprod=xprod*((c-Xlist(j))/(Xlist(i)-Xlist(j)));
end
polyatc = polyatc+Ylist(i)*xprod;
end
disp(polyatc);
end
where polyatc should be the sum of all the y parts of the polynomial estimation for the function value at point x=c and xprod should be the product part of $f(x)*\frac{((x-x_1)(x-x_2)...(x-x_{i-1})(x-x_{i+1})...(x-x_n))}{((x_i-x_1)(x_i-x_2)...(x_i-x_{i-1})(x_i-x_{i+1})...(x_i-x_n))}$
This all looks right to me and looks very similar to ones I have found online, however, when I input lagrange([1,5],[3,11],2) I get 3 when I should get 5 as the subsequent interpolating polynomial is 2x+1. I've tried following the logic of the code but I always come out with 5 and can't figure out where it is going wrong.
This is all being done on MATLAB R2019a for academic use.
Two issues: