(** ESERCIZIO 2.2.2 **)

Clear[f];
f[x_]:=x^4+2x^2-x-3;
Plot[f[x],{x,0,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]

a=0.0;b=2.0;
tol=10^(-10);
fa=f[a];
fb=f[b];
an[0]=a;bn[0]=b;
cn[0]=0.5*(a+b);
test=b-a;
testbis[0]=bn[0]-an[0];
nmax=10;
n=1;While[test>tol,{fa=f[an[n-1]];fc=f[cn[n-1]];If[fa*fc<0,{an[n]=an[n-1];bn[n]=cn[n-1]},{an[n]=cn[n-1];bn[n]=bn[n-1]}];cn[n]=0.5*(an[n]+bn[n]);test=Abs[bn[n]-an[n]];n++}];
Print["Bisezione: n = ", n-1, "  c = ", cn[n-1]]
Clear[x];test=1.0;n=0;
x[0]=0.4;
While[test>tol,{x[n+1]=x[n]-f[x[n]]/f'[x[n]];test=Abs[x[n+1]-x[n]];n++}]
cc=x[n];
Print["Newton: n = ", n, "  c = ", cc]

g1[x_]:=Sqrt[Sqrt[3+x-2x^2]]
g2[x_]:=Sqrt[(x+3-x^4)/2]
g3[x_]:=Sqrt[(x+3)/(x^2+2)]
g4[x_]:=(3x^4+2x^2+3)/(4x^3+4x-1)
Plot[{g1[x],x},{x,0,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g2[x],x},{x,0,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g3[x],x},{x,0,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g4[x],x},{x,0,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]

n=4;
c[0]=1.;
Do[c[k]=g1[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err1=Abs[c[n]-cc]

n=4;
c[0]=1.;
Do[c[k]=g2[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err2=Abs[c[n]-cc]

n=4;
c[0]=1.;
Do[c[k]=g3[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err3=Abs[c[n]-cc]

n=4;
c[0]=1.;
Do[c[k]=g4[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err4=Abs[c[n]-cc]

+

(** ESERCIZIO 2.2.3 **)

cc=N[(21.)^(1/3),10]
Clear[g1,g2,g3,g4]

g1[p_]:=(20.0*p+21./p^2)/21.
g2[p_]:=p-(p^3-21.)/(3.0*p^2)
g3[p_]:=p-(p^4-21.*p)/(p^2-21.)
g4[p_]:=Sqrt[21./p]

n=8;
c[0]=1.;
Do[c[k]=g1[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err1=Abs[c[n]-cc]
Do[c[k]=g2[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err2=Abs[c[n]-cc]
Do[c[k]=g3[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err3=Abs[c[n]-cc]
Do[c[k]=g4[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err4=Abs[c[n]-cc]

(** ESERCIZIO 2.2.4 **)

cc=N[(7.)^(1/5),10]
Clear[g1,g2,g3,g4]

g1[p_]:=Sqrt[1.+(7.-p^3)/p^2]
g2[p_]:=p-(p^5-7.)/p^2
g3[p_]:=p-(p^5-7.)/(5.p^4)
g4[p_]:=p-(p^5-7.)/12.

g1'[cc]
g2'[cc]
g3'[cc]
g4'[cc]

Plot[{g1[x],x},{x,0,3},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g2[x],x},{x,0,3},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g3[x],x},{x,0,3},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g4[x],x},{x,0,3},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]

n=10;
c[0]=1.;
Do[c[k]=g1[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err1=Abs[c[n]-cc]
Do[c[k]=g2[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err2=Abs[c[n]-cc]
Do[c[k]=g3[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err3=Abs[c[n]-cc]
Do[c[k]=g4[c[k-1]];Print["k = ", k, "  c = ",c[k]],{k,1,n}]
err4=Abs[c[n]-cc]

(** ESERCIZIO 2.2.5 **)

Clear[g,f];
f[x_]:=x^4-3x^2-3;
g[x_]:=Sqrt[(x^4-3)/3]
g[x_]:=Sqrt[Sqrt[3x^2+3]]
Plot[f[x],{x,1,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g[x],x},{x,1,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]

a=1.;b=2.;
tol=10^(-10);
fa=f[a];
fb=f[b];
an[0]=a;bn[0]=b;
cn[0]=0.5*(a+b);
test=b-a;
testbis[0]=bn[0]-an[0];
nmax=10;
n=1;While[test>tol,{fa=f[an[n-1]];fc=f[cn[n-1]];If[fa*fc<0,{an[n]=an[n-1];bn[n]=cn[n-1]},{an[n]=cn[n-1];bn[n]=bn[n-1]}];cn[n]=0.5*(an[n]+bn[n]);test=Abs[bn[n]-an[n]];n++}];
Print["Bisezione: n = ", n-1, "  c = ", cn[n-1]]
Clear[x];test=1.0;n=0;
x[0]=0.4;
While[test>tol,{x[n+1]=x[n]-f[x[n]]/f'[x[n]];test=Abs[x[n+1]-x[n]];n++}]
cc=x[n];
Print["Newton: n = ", n, "  c = ", cc]

tol=10.^(-2);nmax=20;
c[0]=1.;test=1.;k=1;
While[test>tol&&k<nmax,{c[k]=g[c[k-1]];test=Abs[c[k]-c[k-1]];Print[k,"   ",test];k++}];
Print["c = ", c[k-1]]
err1=Abs[c[k-1]-cc]

(** ESERCIZIO 2.2.6 **)

Clear[g,f];
f[x_]:=x^3-x-1;
g[x_]:=(x+1)^(1/3)
Plot[f[x],{x,1,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]
Plot[{g[x],x},{x,1,2},PlotStyle->{RGBColor[0,0,1],Thickness[0.005]}]

a=1.;b=2.;
tol=10^(-10);
fa=f[a];
fb=f[b];
an[0]=a;bn[0]=b;
cn[0]=0.5*(a+b);
test=b-a;
testbis[0]=bn[0]-an[0];
nmax=10;
n=1;While[test>tol,{fa=f[an[n-1]];fc=f[cn[n-1]];If[fa*fc<0,{an[n]=an[n-1];bn[n]=cn[n-1]},{an[n]=cn[n-1];bn[n]=bn[n-1]}];cn[n]=0.5*(an[n]+bn[n]);test=Abs[bn[n]-an[n]];n++}];
Print["Bisezione: n = ", n-1, "  c = ", cn[n-1]]
Clear[x];test=1.0;n=0;
x[0]=0.4;
While[test>tol,{x[n+1]=x[n]-f[x[n]]/f'[x[n]];test=Abs[x[n+1]-x[n]];n++}]
cc=x[n];
Print["Newton: n = ", n, "  c = ", cc]

tol=10.^(-2);nmax=20;
c[0]=1.;test=1.;k=1;
While[test>tol&&k<nmax,{c[k]=g[c[k-1]];test=Abs[c[k]-c[k-1]];Print[k,"   ",test];k++}];
Print["c = ", c[k-1]]
err1=Abs[c[k-1]-cc]

(** ESERCIZIO 2.2.7 **)

g[x_]:=Pi+0.5*Sin[0.5*x]

tol=10.^(-10);nmax=20;
c[0]=1.;test=1.;k=1;
While[test>tol&&k<nmax,{c[k]=g[c[k-1]];test=Abs[c[k]-c[k-1]];Print[k,"   ",test];k++}];
Print["c = ", c[k-1], "   ", "g[c]-c = ",g[c[k-1]]-c[k-1] ]