clear
A1=[0.02 0; 0 0.21];
b1=[-0.15 -0.8]';
A2=[0.8496 0.0255; -0.0255 0.8496];
b2=[0 0.15]';
A3=[-0.1554 0.235; 0.1958 0.1865];
b3=[0.05 -0.55]';
A4=[0.1554 -0.235; 0.1958 0.1865];
b4=[-0.35 -0.4]';
f1='A1*x+b1';
f2='A2*x+b2';
f3='A3*x+b3';
f4='A4*x+b4';
x=[-.1; .2];
axis([-1.1 1.1 -1.1 1.1]);
axis square
hold on
set(gca,'Fontsize',12)
title('The Barnsley fern---Figure 11.16','Fontsize',12)
xlabel('{\it x}_1','Fontsize',12)
ylabel('{\it x}_2','Fontsize',12)
h5=plot(x(1),x(2),'erasemode','none');
drawnow
l1=abs(det(A1));
l2=abs(det(A2));
l3=abs(det(A3));
l4=abs(det(A4));
l=[l1 l2 l3 l4];
l_tot=l1+l2+l3+l4;
l=(1/l_tot)*[l1 l2 l3 l4];
for i=1:20000
l_sum=cumsum(l);
tml=find(l_sum-rand > 0);
if size(tml,2)==4
    fe=eval(f1);
  elseif size(tml,2)==3
    fe=eval(f2);
  elseif size(tml,2)==2
    fe=eval(f3);
  else
    fe=eval(f4);
end
x=fe;
set(h5,'xdata',x(1),'ydata',x(2));
drawnow;
end