(** INTERPOLAZIONE **)

n=20;
dx=1.0/n;
x[0]=0;
x[n]=1;
Do[x[i]=x[i-1]+dx,{i,1,n-1}]
Do[Print["i = ", i, "  x[i] = ", x[i]],{i,0,n}]

num[k_,xx_]:=Product[xx-x[i],{i,0,k-1}]*Product[xx-x[i],{i,k+1,n}]
den[k_,xx_]:=Product[x[k]-x[i],{i,0,k-1}]*Product[x[k]-x[i],{i,k+1,n}]
L[k_,xx_]:=num[k,xx]/den[k,xx];
Print["L[k,xx] = ", L[k,xx]]
Plot[L[5,xx],{xx,0,1}]

style1={RGBColor[1,0,0],Thickness[0.008]};
style2={RGBColor[0,0,1],Thickness[0.008]};
ticks1={{0,0.2,0.4,0.6,0.8,1},{-1,0,1}};
ticks2={{-1,-0.5,0,0.5,1},{-1,0,1}};

f[x_]:=Sin[2Pi*x]
Plot[f[x],{x,0,1}]

n=3;
dx=1.0/n;
x[0]=0;
y[0]=f[x[0]];
x[n]=1;
y[n]=f[x[n]];
Do[x[i]=x[i-1]+dx;y[i]=f[x[i]],{i,1,n-1}]
num[k_,xx_]:=Product[xx-x[i],{i,0,k-1}]*Product[xx-x[i],{i,k+1,n}]
den[k_,xx_]:=Product[x[k]-x[i],{i,0,k-1}]*Product[x[k]-x[i],{i,k+1,n}]
L[k_,xx_]:=num[k,xx]/den[k,xx];
p[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
Plot[{p[xx],f[xx]},{xx,0,1},PlotStyle->{style1,style2},Ticks->ticks1]

n=6;
dx=1.0/n;
x[0]=0;
y[0]=f[x[0]];
x[n]=1;
y[n]=f[x[n]];
Do[x[i]=x[i-1]+dx;y[i]=f[x[i]],{i,1,n-1}]
num[k_,xx_]:=Product[xx-x[i],{i,0,k-1}]*Product[xx-x[i],{i,k+1,n}]
den[k_,xx_]:=Product[x[k]-x[i],{i,0,k-1}]*Product[x[k]-x[i],{i,k+1,n}]
L[k_,xx_]:=num[k,xx]/den[k,xx];
p[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
Plot[{p[xx],f[xx]},{xx,0,1.2},PlotStyle->{style1,style2},Ticks->ticks1]

Clear[f];
f[x_]:=Tanh[5x]
Plot[f[x],{x,-1,1},PlotRange->{{-1,1},{-1,1}}]

n=30;
dx=2.0/n;
x[0]=-1;
y[0]=f[x[0]];
x[n]=1;
y[n]=f[x[n]];
Do[x[i]=x[i-1]+dx;y[i]=f[x[i]],{i,1,n-1}]
num[k_,xx_]:=Product[xx-x[i],{i,0,k-1}]*Product[xx-x[i],{i,k+1,n}]
den[k_,xx_]:=Product[x[k]-x[i],{i,0,k-1}]*Product[x[k]-x[i],{i,k+1,n}]
L[k_,xx_]:=num[k,xx]/den[k,xx];
p[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
Plot[{p[xx],f[xx]},{xx,-1,1},PlotStyle->{style1,style2},Ticks->ticks2,PlotRange->{{-1,1},{-2,2}}]

g[x_]:=Tanh[x]
Plot[g[x],{x,-1,1}]
Plot[g''''''[x],{x,-1,1}]