% This script file constructs a single y value for the Lagrange polynomial 
% through the data points given by the xdata and ydata vectors.

% Here is the data set:
xdata=[2 3 4 5];
ydata=[5 6 6 4];

% Give the x value for which we want to know y, and initialize y to zero:
x=4.2;
y=0;

% These nested loops construct the y value. The inner loop creates each of
% the polynomials that is zero at all xdata values but one, and has value
% one at that xdata value. The outer loop forms the linear combination of
% those polynomials one at a time.
for j=1:4
    px=1;
    for k=1:4 
        if k~=j
            px=px*(x-xdata(k))/(xdata(j)-xdata(k));
        end
    end
    y=y+ydata(j)*px;
end

% Here's the y we are looking for:
y

