function myfern(n)
% MYFERN : plot a fractal fern
%
% myfern(n) plots n points.  Default is n =10000

if nargin < 1
    n = 10000;
end;

p  = [ .85  .92  .99  1.00];
A1 = [ .85  .04; -.04  .85];  b1 = [0; 1.6];
A2 = [ .20 -.26;  .23  .22];  b2 = [0; 1.6];
A3 = [-.15  .28;  .26  .24];  b3 = [0; .44];
A4 = [  0    0 ;   0   .16];
x  = [.5 ; .5];

coords = zeros(2,n);
coords(:,1) = x;

hold on;
for i = 2:n
   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;
   end
   coords(:,i) = x;
end

figure;
plot(coords(1,:), coords(2,:), 'g.');