clear
clf

f = 0;
x = [.5; .5]; %initial coordinates 

niter = 80000;
p = [ .1 .83 .93 .95];
A1 = [ 0 0; 0 .25]; b1 = [0; -.14]; %draws the stem
A2 = [ .85 .0; .0 .83]; b2 = [0; 1]; %generates successive copies of the stem and bottom fronds to make the complete fern
A3 = [.09 -.28; .3 .11]; b3 = [0; .6]; %draws bottom frond on left side
A4 = [ -.09 .28 ; .3 .09]; b4 = [0; .7]; %draws bottom frond on right side

for n = 1:niter
    r = rand;
    if r < p(1)
        x = A1*x + b1;
        elseif r < p(2)
            x = A2*x + b2;
            elseif r < p(3)
                x = A3*x + b3;
               else
                    x = A4*x + b4;
    end
    
%     if mod(n + 99,100) == 0;
%         f = f + 1
%         saveas(gcf, ['fern3',sprintf('%03d',f),'.png']);
%     end
    
h = plot(x(1),x(2),'.'); axis([-2 2 0 7]); 
green = [0 3/4 0]; %red, green, blue
set(h,'markersize',5,'color',green);
drawnow
hold on

s = plot(0,5.8,'.');
red = [1 0 0];
set(s,'markersize',60,'color',red);
drawnow
end
