x=[1 4 6];fx=log(x); n=length(x); xx=2; f(xx)=0; %initialize summation for ii=[1:n]; Li=1; %initialize product calculation for j=[1:n]; if(j~=ii); Li=Li*(xx-x(j))/(x(ii)-x(j)); else end end f(xx)=f(xx)+Li*fx(ii); end f(xx)