%On teste la convergence de la méthode de Newton pour "z^3-1=0" dans C
%Ce script s'inspire du script du livre de Lapresté sur Matlab, page 107

[x y] = meshgrid(-1:.01:2,-1:.01:1); z = x+i.*y;
zs = roots([1,0,0,-1]);

%initialisation
tol=0.001; %tolérance
nmax=20;   %nombre maximum d'itérations

im = zeros(size(z)); %l'image initiale
for n=1:nmax
   test = (abs(z-zs(2))>tol) & (abs(z-zs(1))>tol) & (abs(z-zs(3))>tol); %cf. "find"
   %test(i,j) == 1 ssi la convergence n'est pas atteinte en z(i,j)
   if sum(sum(test)) < 2000
      break;
   end;
   z(test) = 2/3*z(test)  + 1./(3*z(test).^2);
   im(test) = im(test) + 1;
end

figure(1) %on place un point d'autant plus clair que la convergence est rapide
fact = max(im(:));
colormap gray
image((1-im/fact)*100);   %points noirs : la convergence n'a pas eu lieue
axis('off');

figure(2) %on dessine les courbes de niveau des nombres d'itérations
[C,h] = contour(x,y,im);
clabel(C,h,[2 4 6 8 12 16 20])
colormap cool
colorbar

clear all