User Tools

Site Tools


gibson:teaching:fall-2012:math445:lab7

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. We are going to create our own code for the game of life and use a different approach based that does not allow anything to live on borders of the domain. We will let our board be 50×50 grid. Start a new script file, and start it with the lines

function mylife(T)
if nargin == 0; T = 500; end 

Look up nargin in the help menu. What is the purpose of this part of the code?

3. Next set up the initial layout for the game of life:

n=100;
n2 = n/2;
L=sparse(n,n);
L(n2-1,n2-1)=1;
L(n2-1,n2)=1;
L(n2,n2-1)=1;
L(n2,n2+1)=1;
L(n2+1,n2+1)=1;
L(n2+1,n2+2)=1;

4. Initialize to zero a variable to count the discrete time t and then start a loop that stops after T iterations or when everything is dead (Hint: what would max(max(L)) return?) What does the command break do?).

5. Implement the code given in the Moler text that will update the L matrix with boundary conditions that repeat the border cells. Later we will change the boundary conditions to enforce zeros on all boundaries.

6. In order to plot the figure, look up the function find in the help menu and use it to determine the x and y indices of the live cells. In the following code the vectors of those indices will be label i and j respectively. Inside the update loop, plot the live cells using the commands

figure(1)
plot(i,j,'ko','MarkerEdgeColor','b','MarkerFaceColor','b','MarkerSize',7);
axis image
axis([0.5, n+0.5, 0.5, n+.5]);
pause

Please briefly describe what each element of the above code does using the help menu. You may want to change the value of 7 above to make your plots look better. If you use pause(0.5), Matlab will pause just half a second, instead of waiting for you to hit return.

Better idea: put the figure(1) command outside the for-loop, in the initialization portion of your code.

7. You should now be able to run the code. If not debug it so that you can.

8. The following code will kill anything on a certain boundary

L(n,:) = 0;

Kill everything on each of the four boundaries in the while loop before you plot. What does the picture look like after a long time? Does it continue changing, or is it constant?

9. The blue circles get a little boring after a while. Use if and elseif conditional statements to color every living cell with 1 or fewer live neighbors red, with 2 live neighbors blue, with 3 live neighbors green, with 4 or more live neighbors the color [0.5 0 0.5]. (Hint: Look up ColorSpec in the help menu). To do this plot each point individually in a for loop. The length of the loop can be found by the size command on one of the vector i or j. You will want to run hold on after at least the first point (every point might be easier) is plotted and then hold off after the last point is plotted.

Better idea: Calculate four sparse matrices L1, L2, L3, L4, which are nonzero wherever L is live and has 1, 2, 3, and 4 or more live neighbors, respectively. Then plot all the nonzero entries of L1 with red, of L2 with blue, L3 with green, and L4 with [0.5 0 0.5].

10. Change the orange color in the above plots to purple. What is the Matlab code for this?

11. You have probably written a code that calculates the number of live neighbors each live cell has twice per loop. Rewrite your code so that it only calculates it once per loop but still accurately computes the next step and accurately displays the color as specified.

12. Modify the update loop so that it will only run until the cell map does not change. To do this you will need to store the previous state of the cell map as another variable and compare (Hint: what would the command max(max(abs(L-P))) return?)

13. (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.

14. (bonus) Modify your code to have periodic boundary conditions instead of zeros on the borders. Does this change the long-term behavior of the system substantially?

15. Turn in your completed code and the answers to the above questions.

gibson/teaching/fall-2012/math445/lab7.txt · Last modified: 2012/10/31 19:34 by gibson