(** DERIVATE - FORMULA A TRE PUNTI E CONFRONTI VARI **)

Clear[f]
f[x_]:=Sin[x];
df[x_]:=Cos[x];

n=32;
h=2.Pi/n;
x[0]=0.0;
x[n]=2.Pi;
Do[x[i]=x[i-1]+h,{i,1,n-1}]
Do[y[i]=f[x[i]],{i,0,n}]
Do[d1[i]=(y[i+1]-y[i-1])/(2*h),{i,1,n-1}]
d1[0]=(-3y[0]+4y[1]-y[2])/(2*h);
d1[n]=(3y[n]-4y[n-1]+y[n-2])/(2*h);
dcalc=Table[{x[i],d1[i]},{i,0,n}];
ticks1={{0,Pi/2,Pi,3Pi/2,2Pi},{-1,0,1}};
pl1=ListPlot[dcalc,PlotStyle->{PointSize[0.02],RGBColor[0,0,1]},Ticks->ticks1];
pl2=Plot[df[x],{x,0,2.Pi},Ticks->ticks1,PlotStyle->{Thickness[0.004],RGBColor[0,0,0]}];
pp1=Show[pl1,pl2]

Show[pp1,pp2]

Clear[f]
f[x_]:=Sin[x+Pi/4];
df[x_]:=Cos[x+Pi/4];

ticks2={{0,0.02,0.04,0.06,0.08},{0.00005,0.0001,0.00015}};
a=0.0;b=2.0;dftrue=df[0.5*(a+b)]
x[0]=a;
nmax=200;
Do[x[n]=b;h=(b-a)/n;hh[n]=h;Do[x[i]=x[i-1]+h,{i,1,n-1}];
Do[y[i]=f[x[i]],{i,0,n}];
Do[d1[i]=(y[i+1]-y[i-1])/(2*h),{i,1,n-1}];
d1[0]=(-3y[0]+4y[1]-y[2])/(2*h);
d1[n]=(3y[n]-4y[n-1]+y[n-2])/(2*h);
err[n]=Abs[d1[n/2]-dftrue],{n,4,nmax,2}]
pl1=ListPlot[Table[{hh[n],err[n]},{n,4,nmax,2}],PlotStyle->RGBColor[0,0,1],PlotRange->{{0,0.1},{0,0.0002}},Ticks->ticks2]

ticks3={{0,0.02,0.04,0.06,0.08},{0.0000005,0.000001,0.0000015}};
a=0.0;b=2.0;
x[0]=a;
nmax=200;
Do[x[n]=b;h=(b-a)/n;hh[n]=h;Do[x[i]=x[i-1]+h,{i,1,n-1}];
Do[y[i]=f[x[i]],{i,0,n}];
Do[d2[i]=(y[i-2]-8y[i-1]+8y[i+1]-y[i+2])/(12*h),{i,2,n-2}];
d2[0]=(-3y[0]+4y[1]-y[2])/(2*h);
d2[1]=(y[2]-y[0])/(2*h);
d2[n-1]=(y[n]-y[n-2])/(2*h);
d2[n]=(3y[n]-4y[n-1]+y[n-2])/(2*h);
err[n]=Abs[d2[n/2]-dftrue],{n,4,nmax,2}]
pl2=ListPlot[Table[{hh[n],1000.*err[n]},{n,4,nmax,2}],PlotStyle->RGBColor[1,0,0],PlotRange->{{0,0.1},{0,0.0002}},Ticks->ticks2]

ticks2={{0,0.02,0.04,0.06,0.08},{0.00005,0.0001,0.00015}};
a=0.0;b=2.0;
x[0]=a;
nmax=200;
Do[x[n]=b;h=(b-a)/n;hh[n]=h;Do[x[i]=x[i-1]+h,{i,1,n-1}];
Do[y[i]=f[x[i]],{i,0,n}];
Do[d3[i]=(y[i+1]-y[i])/h,{i,0,n-1}];
d3[n]=(y[n]-y[n-1])/h;
err[n]=Abs[d3[n/2]-dftrue],{n,4,nmax,2}]
pl3=ListPlot[Table[{hh[n],err[n]/1000.},{n,4,nmax,2}],PlotStyle->RGBColor[0,1,0],Ticks->ticks2]

Show[pl1,pl2,pl3]