function roots1(n,xmin,xmax,ymin,ymax) %ROOTS1 Roots of unity, basin of attraction. % ROOTS1(N) depicts the basin of attraction for the Nth root of unity % by solving Z^N - 1 = 0 using Newton's method with initial conditions % in the complex square [-1,1] x [-1 1]. % % ROOTS1(N,XMIN,XMAX,YMIN,YMAX) depicts basin of attraction in the % complex rectangle [XMIN XMAX] x [YMIN YMAX]. % Francisco J. Beron-Vera % $Revision: 1.0 $ $Date: 2006/09/13 10:07:44 $ if nargin<2 xmin=-1; xmax=1; ymin=-1; ymax=1; end % Defining z^n - 1 and derivative... f=@(z,n) z.^n-1; dfdz=@(z,n) n*z.^(n-1); % n roots of 1. r=exp(2*i*[0:n-1]*pi/n); % Initial conditions (guesses) on a dense rectangular grid. N=250; x=linspace(xmin,xmax,N); y=linspace(ymin,ymax,N); [x y]=meshgrid(x,y); z=x+i*y; % Computing roots from above initial guesses using Newton method... for I=1:50 z=z-f(z,n)./dfdz(z,n); end % Coloring according to attractor reached... w=z; z=repmat(NaN,N); for I=1:n J=find(abs(w-r(I))<1e-3); z(J)=repmat(I,size(J)); end % Plotting... pcolor(x(1,:),y(:,1),z) shading flat axis([xmin xmax ymin ymax]) axis squar