(** EQUAZIONI NONLINEARI **)

f[x_]:=3-9 x-12 (x^2)+4 (x^3)+3 (x^4)
f1[x_]=f'[x]
f2[x_]=f1'[x]
Plot[f[x],{x,-4,3},PlotRange->{-12,8}]

NSolve[f[x]==0,x];
sol=NSolve[f2[x]==0,x]
z1=x/.sol[[1]]
z2=x/.sol[[2]]
f1[z1]
f1[z2]
Plot[f1[x],{x,-3,3},PlotRange->{-6,6}]

eq1=f1[x]==0;
sol1=Solve[eq1,x]//N;
y1=Re[x/.sol1[[1]]]
y2=Re[x/.sol1[[2]]]
y3=Re[x/.sol1[[3]]]
f[y1]
f[y2]
f[y3]

a=y1;b=y1+1.;
tol=10^(-8);
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]=y1+1.;
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]]