%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         9. practice         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Nonlinear dynamics systems  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         2016.11.16.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           Fractals          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Julia set
tic;
% Parameters:
c = 0.36237 + 1i*0.32; %the 1. complex number
felbontas=1000;
x = linspace(-1.2, 1.2, felbontas);    %real part
y = linspace(-1.2, 1.2, felbontas);    %imaginary part
maxiter=600;%600;%100;%150;%
M=zeros(length(x));

%
for k = 1:length(x)
    for l = 1:length(y)
        n = 0;
        z = x(k)+1i*y(l); %the other complex number
        while ((n < maxiter) && (abs(z)<2))
            z = z*z+c; %iteration, decide if the series of z tends to inf or changes periodically or chaoticaly in the complex numeric plain
            n = n+1;
        end
        if (abs(z)<2) %if abs(z)>2, z tends to infinity
            %M(k,l)=1; %sign the position of the proper (finite value) points
            %M(k,l)=abs(z);
        end
        M(k,l)=abs(z);
    end
end
%}

%faster solution with matrix operations
%{
[X,Y]=meshgrid(x,y);
Z=X+1i.*Y;
for i=1:maxiter 
    Z=Z.^2+c;
end
M=abs(Z);
%}

figure;
imagesc(M);
%colormap(bone);
axis xy;
xlabel('Re');
ylabel('Im');
colorbar;
caxis([0,1]);
toc