function newton(n,t)
global M
%  
%  creates an  n x n  matrix M whose (i,j)-th entry contains the
%  number of the root of z^3-1 that the iteration z = z - f(z)/f'(z)
%  converges to,  when started at  z=x+iy
%  where x = (i interpolated to n points between -1.5 and +1.5)
%  and   y = (i interpolated to n points between -1.5 and +1.5)
%
%  Afterwards, M is plotted with 'imagesc(M)' and the 'jet' colormap.
%  You may prefer another colormap; type 'help colormap' to find out more.
%  The list of all colormaps is below; type 'help GRAPH3D' for info.
%
%  Using n=100 and t=6 gives a semi-decent rendering
%  Using n=200 and t=10 gives a better rendering.
%  Using n=300 and t=20 gives a still better rendering.
%  Using n=1000 takes nontrivial time (several minutes cpu), be careful.
%
%  D.S. Parker 1999

x = linspace(-1.5, 1.5, n);
y = linspace(-1.5, 1.5, n);
M = zeros(n,n);

for i=1:n
    for j=1:n
        z = x(i) + y(j)*1i;
        for k=1:t
            z = z - (z^3 - 1)/(3*z^2);
        end;
        r = 0;
        if (real(z)> 0.5) r = 1; end;
        if (imag(z)> 0.25) r = 2; end;
        if (imag(z)<-0.25) r = 3; end;
        M(j,i) = r;
    end;
end;

imagesc(x,y,M)
colormap(hsv)
colorbar('vert') % put the colormap scale on the right
zoom on
while 1
    cmap = -1+MENU('Which colormap would you like?', ...
      'EXIT NOW', ...
      'hsv        - Hue-saturation-value color map', ...
      'hot        - Black-red-yellow-white color map', ...
      'gray       - Linear gray-scale color map', ...
      'bone       - Gray-scale with tinge of blue color map', ...
      'copper     - Linear copper-tone color map', ...
      'pink       - Pastel shades of pink color map', ...
      'white      - All white color map', ...
      'flag       - Alternating red, white, blue, and black color map', ...
      'lines      - Color map with the line colors', ...
      'colorcube  - Enhanced color-cube color map', ...
      'jet        - Variant of HSV', ...
      'prism      - Prism color map', ...
      'cool       - Shades of cyan and magenta color map', ...
      'autumn     - Shades of red and yellow color map', ...
      'spring     - Shades of magenta and yellow color map', ...
      'winter     - Shades of blue and green color map', ...
      'summer     - Shades of green and yellow color map' ...
   );
   if (cmap <= 0) break; end;
   switch(cmap)
      case  1, colormap('hsv')
      case  2, colormap('hot')
      case  3, colormap('gray')
      case  4, colormap('bone')
      case  5, colormap('copper')
      case  6, colormap('pink')
      case  7, colormap('white')
      case  8, colormap('flag')
      case  9, colormap('lines')
      case 10, colormap('colorcube')
      case 11, colormap('jet')
      case 12, colormap('prism')
      case 13, colormap('cool')
      case 14, colormap('autumn')
      case 15, colormap('spring')
      case 16, colormap('winter')
      case 17, colormap('summer')
    end
end;
