cs249 lecture notes, Fall 2001 Week 11, Thursday http://rocky.wellesley.edu/cs249/code/protolife.m Life is the top-level function. There are many annoying things about generating figures in MATLAB. One of them is that there seem to be some options you can set once and some that you have to reset after every iteration. If there is order in this madness, I don't discern it. function life size = 105; A = zeros (size, size); A = center (A, pattern); c = [ 1 1 1; 0 0 0 ]; colormap (c); % you can set this ahead of time for i=1:1000 A = update (A); pcolor (A); shading flat; % but this only after the fact pause (0.01); % we need this to update the figure end You will fill in the body of update as homework. function C = update (A) % compute the next iteration of life using matrix operations C = A; We will fill in the body of updateIter today in lab. function B = updateIter (A) % compute the next iteration of life using for loops B = A; You wrote a version of copymatrix on the Quiz. function result = center (A, B) % copy the matrix B into the middle of matrix a r = round ((size(A,1) - size(B,1)) / 2); c = round ((size(A,2) - size(B,2)) / 2); result = copymatrix (A, B, r, c); function result = copymatrix (A, B, r, c) % copy the matrix B into matrix A at the location r, c height = size (B,1); width = size (B,2); A(r:r+height-1,c:c+width-1) = B; result = A; The function pattern is from a web page that provides a number of Life entities. A program development plan -------------------------- 1) Decide what functions you need and what their interfaces are. For this project I have done that. 2) Write outlines of each of the functions. Usually the outlines contain a single line of code that assigns something simple to the output variable. 3) Develop and test each function in isolation: a) adding a small number of lines at a time b) add print and display commands to generate something visible you can use to test the function 4) assemble the functions into a working whole a) sometimes as you go along, you change the interfaces to some of the functions. When you do this, remember to update all the places where the function is called.