GS 2005/Math - Collins
Computer Worksheet 2
Fractals: MRCM and IFS

Preliminaries

We are generating fractals (self-similar objects) using two
different methods.  Both produce a sequence of images which
when put together form the fractal.

MRCM - Multiple Reduction Copy Machine

   This takes an initial image and makes multiple copies of it which
   are reduced and put on different part of the page.  It then repeats
   this process on the new image, and so on and so on.  The scaling
   and placement of the copies is controlled by a set of transformations.
   Here's a series of three images:



IFS - Iterated Function System

   This takes a starting point and then randomly applies different
   transformations to move the point around.  The final image is
   the collection of all the locations that the point went.
 
Getting Set Up

Start up MATLAB.

Draw a simple figure in the unit square and record the
coordinates in drawing order.  Set up a vector of those
points in pts.  For example, if I want to do a triangle
with coordinates (0,0), (1,0) and (0.5,1), I'd type in MATLAB:

  pts = [0 0
         1 0
        0.5 1
        0 0];

Repeat the first point to make a closed curve.  If you want
a break in your figure, use NaN, NaN as the coordinate.

Now, copy and save these 2 functions:

mrcm.m

function mrcm(pts,N,T)
% routine for MRCM (multiple reduction copy machine)
% pauses between iterations, starts with any image
% an applies the various transformations in N cycles
                                                                                
k = length(T);   % number of transformations
                                                                                
np = [pts'; ones(1,size(pts,1))];   % copy of points (homog)
plot(np(1,:),np(2,:))
axis equal, axis off
pause
for i = 1:N
    xp = [];
    for j = 1:k
        p = T{j}*np;   % apply transformation
        xp = [xp [NaN NaN 1]', p];    % add image to other copies
    end
    np = xp;
    plot(np(1,:),np(2,:))
    axis equal, axis off
    pause
end


ifs.m

function ifs(N,T)
% does N iterations of the IFS defined
% by the transformations passed in
                                                                                
K = length(T);     % number of transformations
X = zeros(3,N);    % allocate space
X(:,1) = [0.5; 0.5; 1]; % starting point

for i = 1:N-1
   r = floor(K*rand)+1;    % random number in 1..K
   X(:,i+1) = T{r}*X(:,i);
end

plot(X(1,:),X(2,:),'.','MarkerSize',1)

axis off


To run these program you need a set of transformations (T).  At the
end of this lab are several sets, just copy and paste the
set you want to use into MATLAB command window.                                                                                
Then type
   
   mrcm(pts,7,T)
   
The function pauses after each iteration; hit the space bar to
see the next iteration (up to 7).  You can use more iterations
but you shouldn't use too many.

To run the IFS function, you just need the transformations and
then type
   
   ifs(10000,T)
   
In this case the function produces 10000 points and will plot the
result when done. You can run again with a larger value, like
100000 or 1000000.


--------------------------TRANSFORMATIONS------------------------------
Copy each to a separate file or into the command window

Fern

clear T
T{1} = [0 0 0; 0 0.16 0; 0 0 1];
T{2} = [0.85 0.04 0; -0.04 0.85 1.6; 0 0 1];
T{3} = [0.2 -0.26 0; 0.23 0.22 1.6; 0 0 1];
T{4} = [-0.15 0.28 0; 0.26 0.24 0.44; 0 0 1]; 

Fractal Tree

clear T
T{1} = [0 0 0; 0 0.5 0;0 0 1];
T{2} = [0.42 -0.42 0; 0.42 0.42 0.2;0 0 1];
T{3} = [0.42 0.42 0; -0.42 0.42 0.2; 0 0 1];
T{4} = [0.1 0 0; 0 0.1 0.2; 0 0 1];


Koch curve

clear T
T{1} = [1/3 0 0; 0 1/3 0; 0 0 1];
T{2} = [1/3*cos(pi/3) -1/3*sin(pi/3) 1/3; 1/3*sin(pi/3) 1/3*cos(pi/3) 0; 0 0 1];
T{3} = [1/3*cos(pi/3)  1/3*sin(pi/3) 1/2; -1/3*sin(pi/3) 1/3*cos(pi/3) sqrt(3)/6; 0 0 1];
T{4} = [1/3 0 2/3; 0 1/3 0; 0 0 1];


Sierpinski Triangle

clear T
T{1} = [1/2 0 0;0 1/2 0;0 0 1];
T{2} = [1/2 0 1/2;0 1/2 1/2; 0 0 1];
T{3} = [1/2 0 1/2; 0 1/2 0; 0 0 1];


Sierpinski Square

clear T
T{1} = [1/3 0 0; 0 1/3 0; 0 0 1];
T{2} = [1/3 0 2/3; 0 1/3 0; 0 0 1];
T{3} = [1/3 0 1/3; 0 1/3 1/3; 0 0 1];
T{4} = [1/3 0 0; 0 1/3 2/3; 0 0 1];
T{5} = [1/3 0 2/3; 0 1/3 2/3; 0 0 1];


Sierpinski Carpet

clear T
T{1} = [1/3 0 0;0 1/3 0;0 0 1];
T{2} = [1/3 0 1/3;0 1/3 0; 0 0 1];
T{3} = [1/3 0 2/3;0 1/3 0; 0 0 1];
T{4} = [1/3 0 0;0 1/3 1/3; 0 0 1];
T{5} = [1/3 0 2/3;0 1/3 1/3; 0 0 1];
T{6} = [1/3 0 0;0 1/3 2/3;0 0 1];
T{7} = [1/3 0 1/3;0 1/3 2/3;0 0 1];
T{9} = [1/3 0 2/3;0 1/3 2/3;0 0 1];


Twin Christmas Tree

clear T
R = [0 -1 0;1 0 0; 0 0 1];   % 90 degree rot
T{1} = [1/2 0 1/2; 0 1/2 0;0 0 1]*R;
T{2} = [1/2 0 1/2;0 1/2 1/2;0 0 1]*inv(R);
T{3} = [1/2 0 1/4;0 1/2 sqrt(3)/4;0 0 1];


3-fold Dragon

clear T
R = [0 1 0;-1 0 0; 0 0 1];   % 270 degree rot
T{1} = [2/3 0 0;0 2/3 2/3;0 0 1]*R;
T{2} = [2/3 0 0;0 2/3 1;0 0 1]*R;
T{3} = [2/3 0 1/3;0 2/3 5/6;0 0 1]*R;


Cantor Maze

clear T
R = [0 -1 0;1 0 0;0 0 1];  % 90 degree rot
T{1} = [1/3 0 1/3;0 1 0;0 0 1]*R;
T{2} = [1/3 0 1/3;0 1/3 2/3;0 0 1];
T{3} = [1/3 0 2/3;0 1 0;0 0 1]*[-1 0 0;0 1 0;0 0 1]*R;


Mail: ccollins@math.utk.edu