function ne=newton(f,f1,x0,eps,nmax),
// recherche d'une solution de l'equation f(x)=0 par la méthode de Newton
// en un nombre maximal nmax d'iterations
// on s'arrete quand la difference de deux valeurs consecutives est < eps 
// f1(x) est la dérivée de f(x)

x=x0,
for n=1:nmax, y=f(x), if y==0 then ne=x, return end
              y1=f1(x), if y1==0 then ne="echec derivee nulle", return end
              D=-y/y1, x=x+D,
              printf("\nx%d = %f f(x%d) = %f", n,x,n,f(x)),
              if abs(D)<eps then 
                 if (y>0)==(f(x)>0) then printf("\nsolution correcte\n")
                 else if (y>0)==f(x+eps)>0 then printf("\nsolution correcte\n")
                      else printf("\nsolution incertaine\n")
                      end
                 end,
                 ne=x, return,
              end,
end,
printf("abandon apres %d iterations avec x = ",nmax),
ne=x, 

// deff('y=f(x)','y=x^2+x-1')
// deff('y=f1(x)','y=2*x+1')
// x0=0
// eps=1E-6
// nmax =1000

// newton(f,f1,x0,eps,nmax)

// x1 = 1.000000 f(x1) = 1.000000
// x2 = 0.666667 f(x2) = 0.111111
// x3 = 0.619048 f(x3) = 0.002268
// x4 = 0.618034 f(x4) = 0.000001
// x5 = 0.618034 f(x5) = 0.000000
// solution correcte
//  ans  = 0.6180340  

// deff('y=f(x)','y=cos(x)-x')
// deff('y=f1(x)','y=-sin(x)-1')

// newton(f,f1,x0,eps,nmax)

// x1 = 1.000000 f(x1) = -0.459698
// x2 = 0.750364 f(x2) = -0.018923
// x3 = 0.739113 f(x3) = -0.000046
// x4 = 0.739085 f(x4) = 0.000000
// x5 = 0.739085 f(x5) = 0.000000
// solution correcte
//  ans  = 0.7390851
  
