1. Read Chapter 12 of the Moler text, and think through how the provided code updates the very first and last cells in the grid. What specific line or lines of code are responsible for these “boundary conditions”? Do the boundary conditions make the domain effectively infinite? Why or why not?
2. Write your own Game of Life code as a Matlab function, following these steps.
Open the file
mylife.m and start enter these lines.
nargin in the help menu. What is the purpose of this part
of the code?
3. Next set up the grid and assign initial layout to the center.
[m,n] = size(X0); % get size of initial condition N2 = floor(N/2); % approx center of grid X=sparse(N,N); % allocate sparse matrix X(N2:N2+m-1, N2:N2+n-1) = X0; % assign X0 to center of X
4. Initialize to zero a variable to count the discrete time
then start a loop that stops after
5. At each time step, plot a marker for each live cell. There are two alternatives for this code.
spy function will plot a marker for each nonzero element of a sparse
matrix, but it's not very flexible with the marker symbols. This will plot
A more complicated but ultimately more satisfying way to plot the live cells
find command, which returns a list of the
x values of nonzero elements of a sparse matrix. This code will
plot a solid blue square at each live cell.
[i,j,x] = find(X); plot(j,i,'bs','MarkerEdgeColor','b','MarkerFaceColor','b','MarkerSize',4); axis ij
Use this latter code unless you can't get your Game of Life code to run correctly.
In that case, use the simpler
spy plotting code to debug, and then replace it
plot once your code is running correctly.
Please briefly describe what each line of your code does. Use the
function or menu if you're not sure.
You may want to change the value of
MarkerSize to make your plots look
better, or change the argument of the
pause function to make the code
run faster or slower. If you use just
pause, with no arguments, Matlab
will pause until you to hit the return key.
6. Implement the code given in the Moler text that will update the
matrix for the next time, using boundary conditions that repeat the border
cells. Later we will change the boundary conditions to enforce zeros (dead cells)
on all of the boundaries.
7. You should now be able to run the code. On your first runs, try using
T = 100; N = 20; X0 = [ 1 1 0 0; 1 0 1 0; 0 0 1 1];
That should lead to some pretty interesting behavior. If you get errors, debug your code until it runs correctly. Save this code as
mylife.m to turn in.
8. Copy that code to
mylife2.m and make further modifications
on that for problems 8-13. Add a few lines of code that stop the time
stepping if every cell dies. (Hint: what would
What does the command
9. Add a few lines of code that stop the time stepping if there are no
changes between successive time steps. (Hint: you'll need to store the
live cells an
Xprev sparse matrix before you update them. Look up
nnz function in help, and use it to compute the number of different
10. The blue circles get a little boring after a while. Let's
color-code the live cells based on how many of their neighbors were
alive at the previous time. Calculate two sparse matrices
which are nonzero wherever
X is live and came from 2 or 3 live
neighbors. Then plot all the nonzero entries of X2 with red and X3
with blue. (Hint: use
hold on to overlay the different colored
hold off to start a new plot at the next time.)
11. Change the red and blue colors above to orange and purple.
(Hint: look up
ColorSpec in the help menu). What is the Matlab
code for this?
12. Experiment with changing the initial condition slightly. Try several small changes. Do small initial changes have small or large effects on the long-term behavior of the system?
13. The following code will kill anything on a certain boundary
X(N,:) = 0;
Kill everything on each of the four boundaries after updating the live cells. Go back to the original initial condition and rerun with the revised boundary conditions. Does that have any effect on the long-term behavior of the system?
14. (bonus) Search the web for interesting initial conditions for the Game
of Life, and figure out an easy way to store an initial condition in a file
and load it into your
Turn in your completed code
mylife2.m codes and the answers to the above questions.