Tuesday, 17 May 2011

Very Simple Fractal

On my way to geek martyrdom (PhD viva approaching) I'm finding myself becoming increasingly interested in fractal functions.

Here's my first attempt at coding a fractal in Matlab, which was done on my lunchbreak today.



It's based on a tiny part of the Mandelbrot set, which I've still not fully read into yet (I'm impatient!).

Here's the code, I told you it was simple! So simple in fact that it..soon...becomes.....pretty..........slow.............

clc         % clear command window
clf         % clear figure

z_old = 0-0i;   % Some random value to initialise z_old

for (seed = 0.01:0.005:0.2) % For all these values
   
    disp(seed)   % Display seed (not the right term)
   
    z0 = -seed - 0.54i; % The juicy bit
    z = 0;              % Start here
   
    rndclr=[rand, rand, rand];  % random line colour
   
    for (i = 1:1:50)   % number of iterations per seed
        z = z^2 + z0;   % iteration for the nation
        %disp(z);
       
        figure(1)
        hold on     % don't delete old lines
        title(i)    % display iteration
        plot(z,'.','color',[0,0.1,0]);  % draw a point
       
        if (i>1) % if point has a predecessor, connect them together
            line([real(z),real(z_old)],[imag(z),imag(z_old)],'Linesmoothing','on','color',rndclr)
        end
       
        z_old = z;  % Store last point
       
    end

end
   

No comments: