(* ESERCIZIO 4.4.11 *)

f1[x_]:=x^3+1;
f2[x_]:=1.001+0.03*(x-0.1)+0.3*(x-0.1)^2+2*(x-0.1)^3;
f3[x_]:=1.009+0.15*(x-0.2)+0.9*(x-0.2)^2+2*(x-0.2)^3;

f[x_]:=If[x≤0.1,f1[x],If[x≤0.2,f2[x],f3[x]]]

Plot[f'''[x],{x,0,0.3}]

f1[0.1]-f2[0.1]
f2[0.2]-f3[0.2]

f1'[0.1]-f2'[0.1]
f2'[0.2]-f3'[0.2]

f1''[0.1]-f2''[0.1]
f2''[0.2]-f3''[0.2]

f1'''[0.1]-f2'''[0.1]
f2'''[0.2]-f3'''[0.2]

f1''''[0.1]-f2''''[0.1]
f2''''[0.2]-f3''''[0.2]

Clear[a,b,x];
a=0.0; b=0.3; n=4;
h=(b-a)/n;
x[0]=a;x[n]=b;
Do[x[i]=x[i-1]+h,{i,1,n-1}];
sum=0.0;
Do[sum+=f[x[i]],{i,1,n-1}];
trap=0.5*h*(f[a]+2.0*sum+f[b])

i1=Integrate[f1[x],{x,0,0.1}]
i2=Integrate[f2[x],{x,0.1,0.2}]
i3=Integrate[f3[x],{x,0.2,0.3}]
true=i1+i2+i3

err=Abs[trap-true]

w[0]=1.;w[n]=1.;
Do[w[i]=4.,{i,1,n-1,2}]
Do[w[i]=2.,{i,2,n-2,2}]
sum=0.0;
Do[sum+=w[i]*f[x[i]],{i,0,n}]
simps=h*sum/3.
err=Abs[simps-true]