function mandelbr
% mandelbr : Mandelbroto fraktalas z_{n+1}=z_n^2+C
% pradedama nuo |z_0|<2 ir randamas pirmas n: |z_n|>4
% tashkui C priskiriama spalva pagal numery n

cmax=256*2; h=[(1:cmax)'/cmax,ones(cmax,1),ones(cmax,1)]; 
rgb=hsv2rgb(h); 
e=sqrt(-1);
ep=10^(-10);
km=0;
%N=257; 
N=65; 
u=zeros(N,N,3); N2=round(N/2); 
for j=-N2:N2, if mod(j,10)==0, disp(j); end; for i=-N2:N2,
  c=1.5*((j/N2-0.5)+e*i/N2); if (abs(c)<2),
  k=0; z=0; p=1; while (k<cmax)&(abs(z)<2)&(p>ep), 
   w=z*z+c; p=(real(w-z))^2+(imag(w-z))^2; z=w; k=k+1; end;
  if (abs(z)>=2)|(p<=ep),
      u(i+N2+1,j+N2+1,:)=rgb(k,:); 
%     u(round((k-(i+N2+1)/N)/cmax*(N-1))+1,j+N2+1,:)=rgb(k,:); 
     km=max(k,km); end;
  end;
%  if abs(c)<1/2, u(i+N2+1,j+N2+1,:)=[1,1,1]; end;
end; end;
image(u); axis equal; axis off;
title('z^2+C');
km