Advertisement
Francoo

Filled Julia Set Generator

Nov 1st, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.01 KB | None | 0 0
  1. % This script generates numerically a Filled Julia/Mandelbrot set of a
  2. % function f(z)=z^2+c, where 'c' is a complex constant (or c=z in
  3. % Mandelbrot).
  4. % TODO: Parallel version (with parfor).
  5.  
  6. %----------------- Parameters:
  7.  
  8. c=complex(-0.8,0.156); % Complex constant that goes on f(z)=z^2+c (for
  9. % Julia only). Try: -0.8,0.156 or -0.74543,0.11301
  10.  
  11. reaxis=[-1.5  1.5]; % Real axis range.
  12. imaxis=[-1  1]; % Imaginary axis range.
  13.  
  14. re_res=1500; % Horizontal resolution, over the real axis.
  15. im_res=1000; % Vertical resolution, over the imaginary axis.
  16. iter=100; % Number of iterations to perform.
  17.  
  18. mandelbrot=false; % true (1) if Mandelbrot, false (0) if Julia.
  19.  
  20. %----------------- Code:
  21.  
  22. h=waitbar(0,'Starting...'); % Progress bar
  23.  
  24. [Re,Im]=meshgrid(linspace(reaxis(1),reaxis(2),re_res), ...
  25.                  linspace(imaxis(2),imaxis(1),im_res));
  26. z=complex(Re,Im); % Creates a complex rectangular plane/grid matrix where
  27. clear Re Im;      % the iterated calculations will be performed
  28.  
  29. if mandelbrot
  30.     zcons=z;
  31. else
  32.     zcons=c;
  33. end
  34.  
  35. itcnt=zeros(im_res,re_res); % Iterations count after becoming NaN/Inf
  36.  
  37. waitbar(0,h,'Calculating...');
  38.  
  39. try
  40.     iter_fun=sym('z^2'); % Function to be performed (z or c is added later)
  41.  
  42.     for k=1:iter
  43.         z=double(subs(iter_fun,z))+zcons; % Performs the function
  44.         jset=logical(isnan(z)); % Checks for NaN values (isnan or isinf?)
  45.         itcnt=itcnt+jset; % Counts iterations after becoming NaN
  46.         waitbar(k/(iter+2),h); % Refresh progress bar
  47.     end
  48. catch
  49.     err_msg = lasterror.message;
  50.     if strfind (err_msg,"sym")
  51.         no_sym_flag=1;
  52.     end
  53. end
  54.  
  55. if no_sym_flag
  56.     for k=1:iter
  57.         z=z.^2+zcons; % Performs the function
  58.         jset=logical(isnan(z)); % Checks for NaN values (isnan or isinf?)
  59.         itcnt=itcnt+jset; % Counts iterations after becoming NaN
  60.         waitbar(k/(iter+2),h); % Refresh progress bar
  61.     end
  62. end
  63.  
  64. %----------------- Post-processing:
  65.  
  66. waitbar(k/(iter+1),h,'Post-processing...');
  67.  
  68. itper=(1-itcnt/iter)/(1-min(min(itcnt))/iter); % Normalized inetation
  69. % counts (range: [0,1]).
  70. zfix=abs(z); % Normalized z data (without NaN and Inf).
  71. zfix(isnan(zfix))=0;
  72. itper(itper==1)=0;
  73. prettyfig=zfix+itper; % Combination of zfix and itper, displaying purpose.
  74. prettyfig(prettyfig>1)=1;
  75.  
  76. waitbar(1,h,'Done...');
  77.  
  78. %----------------- Displaying the images:
  79. imtool(jset);
  80. imtool(prettyfig);
  81.  
  82. %----------------- Saving the images:
  83. numb=dir('prettyfig*.png'); % Detecting largest file number
  84. if size(numb,1) ~= 0
  85.     numb=struct2cell(numb); numb=numb(1,:);
  86.     y=regexp(numb,'[0-9]{3}');
  87.     numb=cell2mat(numb');
  88.     numb=num2str(nanmax(str2num(numb(:,y{1}:y{1}+2))+1),'%03u');
  89. else
  90.     numb='000';
  91. end
  92. imwrite(uint8(-1+prettyfig*64),pink,['prettyfig' numb '.png']) % Strangely this
  93. imwrite(uint8(-1+jset*64),gray,['juliaset' numb '.png']) % opens an empty figure
  94.  
  95. %----------------- Clearing used variables:
  96. % clear c im_res imaxis itcnt iter itper jset k prettyfig re_res
  97. % clear reaxis z zfix
  98.  
  99. close(h);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement