
% Iterated function system
% Ricardo Carretero Mar/03

f1 = inline('[1/3*(2+x) 1/3*(2+y)]','x','y');
f2 = inline('[1/3*(2+x) 1/3*y]','x','y');
f3 = inline('[1/3*x 1/3*(2+y)]','x','y');
f4 = inline('[1/3*x 1/3*y]','x','y');

Npts=1000;
Nit=10;
x=pi/30*0+0.4;		% ICs
y=x*pi/4*0+0.4;
ms=1;
fs=15;

figure(1)
clf
axis([0 1 0 1]);
set(gca,'FontSize',[fs]);
xlabel('x_n'); ylabel('y_n');


p1 = plot(x,y,'r.','EraseMode','none','MarkerSize',ms);
axis([0 1 0 1]);

X=[x];
Y=[y];

for i=1:Nit
 X1=[];
 Y1=[];
 for i=1:Npts
  r=rand(1)*4;
  if(r<1)
   F = f1(x,y);
  elseif(r<2)
   F = f2(x,y);
  elseif(r<3)
   F = f3(x,y);
  else
   F = f4(x,y);
  end
  x=F(1);
  y=F(2);
  X1=[X1,x];
  Y1=[Y1,y];
 end
 set(p1,'Xdata',X1,'YData',Y1)
 drawnow;
 X=[X,X1];
 Y=[Y,Y1];
end

Ntrans=10;

plot(X,Y,'r.','EraseMode','none','MarkerSize',ms);
hold on
plot(X(1:1),Y(1:1),'ko','EraseMode','none','MarkerSize',8);
plot(X(1:Ntrans),Y(1:Ntrans),'b*','EraseMode','none','MarkerSize',8);

for ii=1:Ntrans
 text(X(ii)+0.01,Y(ii)-0.01,num2str(ii))
end

drawnow;
hold off

