(** EQUAZIONI NONLINEARI ESEMPIO **)

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

a=0.0;b=1.0;
tol=10^(-4);
fa=f[a];
fb=f[b];
an[0]=a;bn[0]=b;
cn[0]=0.5*(a+b);
testbis[0]=bn[0]-an[0];
nmax=10;
Do[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]);testbis[n]=Abs[bn[n]-an[n]],{n,1,nmax}];
Clear[x];
nmax=10;x[0]=4.;
Do[x[n+1]=x[n]-f[x[n]]/f'[x[n]];testnr[n]=Abs[x[n+1]-x[n]],{n,0,nmax}];

xnr=Table[x[n],{n,0,nmax}]
xbi=Table[cn[n],{n,0,nmax}]
errbis=Table[testbis[n],{n,0,nmax}]
errnr=Table[testnr[n],{n,0,nmax}]

ListPlot[{xbi,xnr},Joined->True,PlotRange->{0,0.4}]
ListPlot[{errbis,errnr},Joined->True,PlotRange->{0,0.4}]

Do[Print["n= ",n,"  c[n] = ", cn[n],"  x[n] = ", x[n],"  testbis = ",testbis[n],"  testnr = ", testnr[n]],{n,0,nmax}];

Clear[f];
f[x_]:=Exp[x]-3x^2
Plot[f[x],{x,0,1}]

a=0.0;b=1.0;
tol=10^(-5);
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++}]
Print["Newton: n = ", n, "  c = ", x[n]]

Do[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]);testbis[n]=Abs[bn[n]-an[n]],{n,1,nmax}];
Clear[x];
x[0]=0.6;
Do[x[n+1]=x[n]-f[x[n]]/f'[x[n]];testnr[n]=Abs[x[n+1]-x[n]],{n,0,nmax}];

Do[Print["n= ",n,"  c[n] = ", cn[n],"  x[n] = ", x[n],"  testbis = ",testbis[n],"  testnr = ", testnr[n]],{n,0,nmax}];

Plot[f[x],{x,3,5}]