User Tools

Site Tools


gibson:teaching:fall-2012:math445:lab7

**This is an old revision of the document!** ----

A PCRE internal error occured. This might be caused by a faulty plugin

====== Math 445 Game of Life Lab ====== **1.** Read [[http://www.mathworks.com/moler/exm/chapters/life.pdf|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 100x100 grid. Start a new script file, and start it with the lines <code> function mylife(T) if nargin == 0; T = 500; end </code> 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: <code> 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; </code> **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 Chapter 9 that will update the ''L'' matrix with periodic boundary conditions. Later we will change it to not be periodic. **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 <code> 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 </code> 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. **7.** You should now be able to run the code. If not debug it so that you can. Verify that the boundaries are periodic and wrap from one edge of the screen to the other. **8.** The following code will kill anything on a certain boundary <code> L(n,:) = 0; </code> 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. **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.** Turn in your completed code and the answers to the above questions.

gibson/teaching/fall-2012/math445/lab7.1350990229.txt.gz · Last modified: 2012/10/23 04:03 by gibson