(** ESERCIZIO 3.1.3 e 7 (a) **)

n=3;
x[0]=8.1; x[1]=8.3;x[2]=8.6;x[3]=8.7;
y[0]=16.94410; y[1]=17.56492;y[2]=18.50515;y[3]=18.82091;
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]}];
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];
p3[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pts={{8.4,p3[8.4]}};
pl2=ListPlot[pts,PlotStyle->{PointSize[0.02],RGBColor[0,1,0]}];
pl3=Plot[p3[xx],{xx,8.1,8.7}];
pl4=Plot[xx*Log[xx],{xx,8.1,8.7}];
Show[pl1,pl2,pl3,pl4]

n=2;
x[0]=8.1; x[1]=8.3;x[2]=8.6;
y[0]=16.94410; y[1]=17.56492;y[2]=18.50515;
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]}];
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];
p2[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pts={{8.4,p3[8.4]}};
pl2=ListPlot[pts,PlotStyle->{PointSize[0.02],RGBColor[0,1,0]}];
pl3=Plot[p2[xx],{xx,8.1,8.7}];
pl4=Plot[xx*Log[xx],{xx,8.1,8.7}];
Show[pl1,pl2,pl3,pl4]

n=1;
 x[0]=8.3;x[1]=8.6;
 y[0]=17.56492;y[1]=18.50515;
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]}];
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];
p1[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pts={{8.4,p3[8.4]}};
pl2=ListPlot[pts,PlotStyle->{PointSize[0.02],RGBColor[0,1,0]}];
pl3=Plot[p1[xx],{xx,8.1,8.7}];
pl4=Plot[xx*Log[xx],{xx,8.1,8.7}];
Show[pl1,pl2,pl3,pl4]

(** ESERCIZIO 3.1.3 e 7 (b) **)

n=3;
x[0]=-0.75; x[1]=-0.5;x[2]=-0.25;x[3]=0.;
y[0]=-0.07181250; y[1]=-0.02475;y[2]=0.3349375;y[3]=1.101;
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{-1,0},{-0.2,1.5}}];
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];
p3[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pts={{-1./3.,p3[-1./3.]}};
pl2=ListPlot[pts,PlotStyle->{PointSize[0.02],RGBColor[0,1,0]},PlotRange->{{-1,0},{-0.2,1.5}}];
pl3=Plot[p3[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.5}}];
f[x_]:=x^3+4.001*x^2+4.002*x+1.101;
pl4=Plot[f[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.5}},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl2,pl3,pl4]

n=2;
x[0]=-0.75; x[1]=-0.5;x[2]=-0.25;
y[0]=-0.07181250; y[1]=-0.02475;y[2]=0.3349375;
(* x[0]=-0.5;x[1]=-0.25;x[2]=0.;
 y[0]=-0.02475;y[1]=0.3349375;y[2]=1.101;*)
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{-1,0},{-0.2,1.2}}];
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];
p3[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pts={{-1./3.,p3[-1./3.]}};
pl2=ListPlot[pts,PlotStyle->{PointSize[0.02],RGBColor[0,1,0]},PlotRange->{{-1,0},{-0.2,1.2}}];
pl3=Plot[p3[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.2}}];
f[x_]:=x^3+4.001*x^2+4.002*x+1.101;
pl4=Plot[f[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.2}},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl2,pl3,pl4]

n=2;
x[0]=-0.75; x[1]=-0.5;x[2]=0.;
y[0]=-0.07181250; y[1]=-0.02475;y[2]=1.101;
(* x[0]=-0.5;x[1]=-0.25;x[2]=0.;
 y[0]=-0.02475;y[1]=0.3349375;y[2]=1.101;*)
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{-1,0},{-0.2,1.2}}];
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];
p3[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pts={{-1./3.,p3[-1./3.]}};
pl2=ListPlot[pts,PlotStyle->{PointSize[0.02],RGBColor[0,1,0]},PlotRange->{{-1,0},{-0.2,1.2}}];
pl3=Plot[p3[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.2}}];
f[x_]:=x^3+4.001*x^2+4.002*x+1.101;
pl4=Plot[f[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.2}},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl2,pl3,pl4]

n=1;
 x[0]=-0.5;x[1]=-0.25;
 y[0]=-0.02475;y[1]=0.3349375;
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{-1,0},{-0.2,1.2}}];
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];
p3[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pts={{-1./3.,p3[-1./3.]}};
pl2=ListPlot[pts,PlotStyle->{PointSize[0.02],RGBColor[0,1,0]},PlotRange->{{-1,0},{-0.2,1.2}}];
pl3=Plot[p3[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.2}}];
f[x_]:=x^3+4.001*x^2+4.002*x+1.101;
pl4=Plot[f[xx],{xx,-1,0},PlotRange->{{-1,0},{-0.2,1.2}},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl2,pl3,pl4]

(** ESERCIZIO 3.2.4 (a) **)

Clear[f]
n=4;
x[0]=0;f[0]=-6.0;
x[1]=0.1;f[1]=-5.89483;
x[2]=0.3;f[2]=-5.65014;
x[3]=0.6;f[3]=-5.17788;
x[4]=1.0;f[4]=-4.28172;
ff=Table[{x[k],f[k]},{k,0,n}]

ticks1={{0,0.2,0.4,0.6,0.8,1},{-6,-5,-4,-3}};
pl1=ListPlot[ff,PlotStyle->PointSize[0.02],Ticks->ticks1];

(* ALGORITMO 3.2 del Faires Burden *)
Do[d[k][0]=f[k],{k,0,n}]
Do[d[k][j]=(d[k][j-1]-d[k-1][j-1])/(x[k]-x[k-j]),{k,1,n},{j,1,k}]
pn[x_]=Sum[d[k][k]*Product[(x-x[j]),{j,0,k-1}],{k,0,n}];
pl2=Plot[pn[x],{x,0,1.2}];

Show[pl1,pl2,PlotRange->{{0,1.2},{-6,-3}}]

(* ESERCIZIO 3.2.15 *)

n=2;
f[xx_]:=Exp[2*xx]*Cos[3*xx]
x[0]=0.0; x[1]=0.3;x[2]=0.6;
y[0]=f[x[0]]; y[1]=f[x[1]];y[2]=f[x[2]];
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{-0.2,0.8},{-2,2}}];
pl4=Plot[f[xx],{xx,-0.2,0.8},PlotRange->{{-0.2,0.8},Automatic},PlotStyle->RGBColor[0,0,0]];
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];
p2[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pl3=Plot[p2[xx],{xx,-0.2,0.8},PlotRange->{{-0.2,0.8},Automatic},PlotStyle->RGBColor[0,1,0]];
pl4=Plot[f[xx],{xx,-0.2,0.8},PlotRange->{{-0.2,0.8},Automatic},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl3,pl4]

n=2;
f[xx_]:=Sin[Log[xx]];
x[0]=2.0; x[1]=2.4;x[2]=2.6;
y[0]=f[x[0]]; y[1]=f[x[1]];y[2]=f[x[2]];
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{1.8,2.8},{0,1}}];
pl4=Plot[f[xx],{xx,1.8,2.8},PlotRange->{{1.8,2.8},{0,1}},PlotStyle->RGBColor[0,0,0]];
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];
p2[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pl3=Plot[p2[xx],{xx,1.8,2.8},PlotRange->{{1.8,2.8},{0,1}},PlotStyle->RGBColor[0,1,0]];
pl4=Plot[f[xx],{xx,1.8,2.8},PlotRange->{{1.8,2.8},{0,1}},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl3,pl4]

n=3;
f[xx_]:=Log[xx];
x[0]=1.0; x[1]=1.1;x[2]=1.3;x[3]=1.4;
y[0]=f[x[0]]; y[1]=f[x[1]];y[2]=f[x[2]];y[3]=f[x[3]];
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{0.8,1.6},{-0.1,0.5}}];
pl4=Plot[f[xx],{xx,0.8,1.6},PlotRange->{{0.8,1.6},{-0.1,1}},PlotStyle->RGBColor[0,0,0]];
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];
p2[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pl3=Plot[p2[xx],{xx,0.8,1.6},PlotRange->{{0.8,1.6},{-0.1,0.5}},PlotStyle->RGBColor[0,1,0]];
pl4=Plot[f[xx],{xx,0.8,1.6},PlotRange->{{0.8,1.6},{-0.1,0.5}},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl3,pl4]

n=3;
f[xx_]:=Cos[xx]+Sin[xx];
x[0]=0.0; x[1]=0.25;x[2]=0.5;x[3]=1.0;
y[0]=f[x[0]]; y[1]=f[x[1]];y[2]=f[x[2]];y[3]=f[x[3]];
ff=Table[{x[i],y[i]},{i,0,n}];
pl1=ListPlot[ff,PlotStyle->{PointSize[0.02],RGBColor[1,0,0]},PlotRange->{{-0.2,1.2},{0.8,1.5}}];
pl4=Plot[f[xx],{xx,-0.2,1.2},PlotRange->{{-0.2,1.2},{0.8,1.5}},PlotStyle->RGBColor[0,0,0]];
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];
p2[xx_]:=Sum[y[k]*L[k,xx],{k,0,n}]
pl3=Plot[p2[xx],{xx,-0.2,1.2},PlotRange->{{-0.2,1.2},{0.8,1.5}},PlotStyle->RGBColor[0,1,0]];
pl4=Plot[f[xx],{xx,-0.2,1.2},PlotRange->{{-0.2,1.2},{0.8,1.5}},PlotStyle->RGBColor[0,0,0]];
Show[pl1,pl3,pl4]