Interpolation

y - values are given at x-values 0,1,2,3. A third degree polynomial is sought which passes through the y-values. It has coefficients coeff[[i]], i=1,2,3,4, giving curve[t]=coeff[[1]] + t*coeff[[2]] + t^2*coeff[[3]] + t^3*coeff[[4]]. The soltion is obtained by setting up a linear system of equations and solving it for the coeff[[i]].

interpol[data_]:=
(
matrix={{1.0,0,0,0},{1,1,1,1},{1,2,4,8},{1,3,9,27}};
coeff=LinearSolve[matrix,data];

Print[MatrixForm[matrix],MatrixForm[coeff],"=",MatrixForm[data]];
(* we have found the solution; the rest is plotting. *)

points=Graphics[{PointSize[0.015],Point[{{0,data[[1]]},{1,data[[2]]},{2,data[[3]]},{3,data[[4]]}}]}];

curve[t_]:=coeff[[1]]+t*coeff[[2]]+t^2*coeff[[3]]+coeff[[4]]t^3;

cplot=Plot[curve[t],{t,0,3},PlotStyle->{Thick,Green}];

Show[{cplot,points},PlotRange->{{0,3},{-1,3}},AxesOrigin->{0,0},ImageSize->300]
);

data={1,2,2,1};
interpol[data];

data={-1,2,0,0};
interpol[data];

data={-1,0,1,2};
interpol[data];