% Find a polynomial curve y=P\left(x\right) that passes through three points \left(1,2\right),\left(2,-1\right),\left(4,3\right).

xdata = [1,2,4];
ydata = [2,-1,3];

% Define the Lagrange interpolation polynomial P\left(x\right):

syms x %declare that x is a symbolic variable
L0 = @(x) (x-2)*(x-4)/((1-2)*(1-4));
L1 = @(x) (x-1)*(x-4)/((2-1)*(2-4));
L2 = @(x) (x-1)*(x-2)/((4-1)*(4-2));
P = @(x) 2*L0(x)+(-1)*L1(x)+3*L2(x);

% If you want the polynomial to be in standard form (sum of descending power of x, use the command:

simplify(P(x))

%  Plot the polynomial P\left(x\right) on interval \left\lbrack 0\ldotp 5,\;4\ldotp 5\right\rbrack together with the three given data points.

fplot(P,[.5,4.5]);
hold on
plot(xdata,ydata,'*')