function y=dicho(fonc,a,b,N)

deff('y=h(x)',fonc);
c=(a+b)/2;
if (N==0 | h(a)*h(b)>0) then y=c; 
elseif h(a)*h(c)>0 then y=dicho(fonc,c,b,N-1);
else y=dicho(fonc,a,c,N-1);
end;

endfunction;

//-----------------------------------------------

function y=newton(fonc,a,N)

delta=10^(-5);
deff('y=h(x)',fonc);
if N==0 then y=a; 
else 
	deriv=(h(a+delta)-h(a))/delta;
	if deriv==0 then deriv=10^(-100); end;
	b=a-h(a)/deriv;
	y=newton(fonc,b,N-1);
end;

endfunction;

//------------------------------------------------

function vitesse();    //NB : cette fonction peut etre grandement acceleree. Pourquoi ?

n=20;
f='y=x^2-2';
xexact=sqrt(2);
a=0;b=10;

xgraph=1:n;
ygraph=zeros(2,n);
for i=1:n
		xd=dicho(f,a,b,i); xn=newton(f,b,i);
		if xd-xexact==0 then xd=xexact+10^(-15); end;
		if xn-xexact==0 then xn=xexact+10^(-15); end;
		ygraph(1,i)=-log(abs(xd-xexact));
		ygraph(2,i)=-log(abs(xn-xexact));
end;

clf();
plot2d([xgraph;xgraph]',ygraph',rect=[0,0,n,30]);

endfunction;

//-------------------------------------------------

function newtonsinus();

xmin=0;xmax=8; npoints=500;
nitere=10;

xgraph=linspace(xmin,xmax,npoints);
yzero=zeros(1,npoints);
ygraph=zeros(1,npoints);
for i=1:npoints  
		ygraph(i)=newton('y=sin(%pi*x)',xgraph(i),nitere);
end;

clf(); ysin=(xmax/2)*sin(%pi*xgraph);
plot2d([xgraph;xgraph;xgraph]',[ygraph;ysin;yzero]',rect=[xmin,-xmax/2,xmax,xmax]);

endfunction;
