//
// Recherche de la plus grande racine de f
//
deff('y=f(x)','y=x^3-x');
deff('y=fprime(x)','y=3*x^2-1');
xset('window',0);
clf
x=0.1:0.01:3;
plot2d([x',x'],[f(x'),zeros(x')],[1,4]);
//
// methode de dichotomie (bissection)
//
xset('window',1);
clf
a=0.5;b=2;c=(a+b)/2;nb=0;
errb=[];
y1=f(a);y2=f(b);
while (f(c)~=0)&(nb < 100)
  c=(a+b)/2;
  if (f(c)*f(a)<0) then
    b=c;
  else
    a=c;
  end 
  nb=nb+1;
  errb=[errb;abs(c-1)];
end 
plot2d1('gnl',(0:1:nb-2)',errb(1:(nb-1)))

//
// methode de dichotomie (corde)
//
a=0.5;b=2;nfp=0;
y1=f(a);y2=f(b);c=b-y2*(b-a)/(y2-y1);
errfp=[];
while (f(c)~=0)&(y2~=y1)&(nfp < 100)
  c=b-y2*(b-a)/(y2-y1);
  if (f(c)*f(a)<0) then
    b=c;y2=f(c);
  else
    a=c;y1=f(c);
  end 
  nfp=nfp+1;
  errfp=[errfp;abs(c-1)];
end 
plot2d1('gnl',(0:1:nfp-2)',errfp(1:(nfp-1)),2,'001');
//
// methode de Newton
//
x0=2;err=[abs(x0-1)];nn=1;
while (f(x0)~=0)&(fprime(x0)~=0)
  x0=x0-f(x0)/fprime(x0);
  nn=nn+1;
  err=[err;abs(x0-1)];
end
plot2d1('gnl',(0:1:nn-2)',err(1:(nn-1)),5,'001');
xtitle('erreur de la methode de bissection (noire) corde (bleue) et Newton (rouge)','n','erreur');
