function chaos(N) % The input variable represents the number of iterations % made by the chaos game. % Close all open figure windows. Delete this line if you % don't want to do this. close all; % Create a new figure window, axes, and scale the axes. fig1=figure; ax1=axes; axis([0,1,0,1]) % Place a message on the screen for the user. txt1=text(.1,.5,'Select three points vertices with the mouse.'); % This command allows user to select vertices with the mouse. [x,y]=ginput(3); A=[x(1);y(1)]; B=[x(2);y(2)]; C=[x(3);y(3)]; % Delete the text message. delete(txt1) % Plot and label vertices selected by user. text(A(1),A(2),'A') text(B(1),B(2),'B') text(C(1),C(2),'C') % Place a text message on the screen for the user. txt2=text(.1,.5,'Select initial point.'); % This command allows user to select initial point with the mouse. x=ginput(1); x=x(:); % Makes x a column vector % Delete the text message delete(txt2); % Main algorithm begins. The idea is this: Roll a 3-sided % die with the letters A, B, and C on the faces. If A comes % up, plot the midpoint of the segment joining the current % point with the point A. Similar comments apply if B or C % are rolled. % Reserve space for the N points (2 by 1 column vectors) % produced by the following iteration. X=zeros(2,N); % Main loop begins for i=1:N % The function rand produces a random number between 0 and 1. r=rand; % Roll the die if r<1/3 x=mid(x,A); % A was rolled. Find midpoint of current point and A. X(:,i)=x; % store the result for later plotting elseif r<2/3 x=mid(x,B); % B was rolled. Find midpoint of current point and B. X(:,i)=x; % store the result for later plotting else x=mid(x,C); % C was rolled. Find midpoint of current point and C. X(:,i)=x; % store the result for later plotting end % end if end % end for loop line(X(1,:),X(2,:),... 'linestyle','none',... 'marker','.',... 'markersize',1); function u = mid(x,vert) % Find the midpoint of the segment joining current point and vert. u=0.5*(x+vert);