# channelflow.org

### Site Tools

gibson:teaching:spring-2018:math445:lab10

# Math 445 Game of Life Lab

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.

```function mylife(X0,T,N)
if nargin == 2; N = 20; end
if nargin == 1; T = 100; end ```

Look up `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 `t` and then start a loop that stops after `T` iterations

5. At each time step, plot a marker for each live cell. There are two alternatives for this code.

The `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 blue squares

`spy(X,'bs'); `

A more complicated but ultimately more satisfying way to plot the live cells uses the `find` command, which returns a list of the `i,j` coordinates and 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 with `find` and `plot` once your code is running correctly.

```axis([0.5, n+0.5, 0.5, n+.5]);
xlabel('j')
ylabel('i')
pause(0.25)
title(strcat('t=',int2str(t)));```

Please briefly describe what each line of your code does. Use the `help` 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 `X` 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 `max(max(X))` return?) What does the command `break` do?)

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 the `nnz` function in help, and use it to compute the number of different elements between `X` and `Xprev`.)

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 `X2, X3,` 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 plots and `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 `mylife` code.

Turn in your completed code `mylife.m` and `mylife2.m` codes and the answers to the above questions. 