CPI 200, G. Farin

Interpolation

The interpolation problem fits a polynomial to a set of data.

Linear interpolation

The task is to fit a linear funtion linint(x) to two points (0,(Subscript[y, 0])) and (1,(Subscript[y, 1])).

points={{0,3},{1,2}}; (* need first arguments 0 and 1 forListPlot *)
linint[x_]:= (1-x)*points[[1,2]]+x*points[[2,2]];
pic1=ListPlot[points,PlotStyle->Directive[{PointSize[Large],Red}]];
pic2= Plot[linint[x],{x,0,1},PlotStyle-> Thick];

Show[{pic2,pic1}]

polynomial interpolation

n=3;
(* a sample of data sets: *)
datasets={Table[Sin[3i],{i,1,n+1}]//N,
Table[Abs[i-1-n/2],{i,1,n+1}]//N,
Table[Sign[i-1-n/2],{i,1,n+1}]//N,
Table[i^2/(i-n/2),{i,1,n+1}]//N,
Table[Log[15i]+1,{i,1,n+1}],
{1,0,0,0}}//N;

data=datasets[[5]]; (* pick one data set *)
points=Table[{(i-1)/n,data[[i]]},{i,1,n+1}]//N

The Lagrange polynomials:

(Subscript[L, 0]^3)(x) =1· ((x-(Subscript[x, 1]))/((Subscript[x, 0])-(Subscript[x, 1]))) ((x-(Subscript[x, 2]))/((Subscript[x, 0])-(Subscript[x, 2]))) ((x-(Subscript[x, 3]))/((Subscript[x, 0])-(Subscript[x, 3])))
(Subscript[L, 1]^3)(x) = ((x-(Subscript[x, 0]))/((Subscript[x, 0])-(Subscript[x, 1]))) ·1·((x-(Subscript[x, 2]))/( ·(Subscript[x, 0])-(Subscript[x, 2]))) ((x-(Subscript[x, 3]))/((Subscript[x, 0])-(Subscript[x, 3])))
(Subscript[L, 2]^3)(x) = ((x-(Subscript[x, 0]))/((Subscript[x, 0])-(Subscript[x, 1]))) ((x-(Subscript[x, 1]))/((Subscript[x, 0])-(Subscript[x, 2]))) · 1·((x-(Subscript[x, 3]))/((Subscript[x, 0])-(Subscript[x, 3])))
(Subscript[L, 3]^3)(x) = ((x-(Subscript[x, 0]))/((Subscript[x, 0])-(Subscript[x, 1]))) ((x-(Subscript[x, 1]))/((Subscript[x, 0])-(Subscript[x, 2]))) ((x-(Subscript[x, 2]))/((Subscript[x, 0])-(Subscript[x, 3])))·1

fac[i_,j_,x_]:= If[i==j,1,n*(x-j/n)/(i-j)];
lagrange[n_,i_,x_]:= Product[fac[i,j,x],{j,0,n}];

(* interpolant *)
intfunc[n_,data_,x_]:=Sum[lagrange[n,i-1,x]*data[[i]],{i,1,n+1}];


Print["intfunc = ",intfunc[n, data, 0.0]];
Print["lagrange = ",lagrange[n, 0, 0.0]];

Table[
Plot[lagrange[n,j,x],{x,0,1},PlotStyle-> Thick,PlotRange->2.5],{j,0,n,1}]


pic1=Plot[intfunc[n,data,x],{x,0,1},PlotStyle-> Thick];
pic2=ListPlot[points,PlotStyle->Directive[Red,PointSize[Large]]];


pic=Show[{pic1, pic2}]

Hermite interpolation

Given: function values y0 and y1 and associated slopes s0 and s1. 
Find: a cubic h(x) s.t. h(0)=y0, h(1)=y1, h’(0)=s0, h’(1)=s1.

Our cubic may be written as
	h(x)= y0 ((1-x)^3 + 3(1-x)^2 x)
		+ s0 ((x-1)^2 x)
		+ s1 (1-x) x^2
		+ y1(x^3 + 3(1-x)x^2).

s0=1;
s1=1;
y0 = 3.0;
y1=1;

h[x_]:=y0 *((1-x)^3+3*(1-x)^2 *x)+s0*(1-x)^2*x + s1*(x-1)*x^2 + y1*(x^3 +3*(1-x)*x^2);

points={{0,y0},{1,y1}};

picp=ListPlot[points,PlotStyle->Directive[PointSize[Large]]];
pich= Plot[h[x],{x,0,1},PlotStyle-> {Thick,Green}];
picder=Plot[h''[x],{x,0,1},PlotStyle-> Thick];

Show[{picder,pich,picp}]