Cellular automata We discuss cellular automata as a simple application of MATLAB programming and as an accessible scientific topic of recent interest. You can find a lot of information on the internet. In particular, you can visit the website of Wolfram s book, "A New Kind of Science" at www.wolframscience.com Cellular automata are a kind of abstract computing machine whose state evolves in discrete time steps according to a fixed set of rules. There is an array of cells, and at any given time each cell is in one of a finite number of states - for simplicity we will use only two states, designated by zero/one or "off/on" or "white/black". A cell in state "one" will be considered "on" and is represented visually by a black cell; if the state is zero, the cell is "off" and is represented visually by a white (or empty) cell. At each time step the state of a cell changes in a manner that depends on the values of its state and the states of its "neighboring" cells, according to a fixed rule, or function. As an example, consider cells laid out in a two-dimensional rectangular grid and consider the following rule, used by Conway in the so-called "Game of Life": At time n 1 a given cell is "on" if at time n a) three out of its surrounding eight nearest neighbors are "on" or b) two of its surrounding eight nearest neighbors are "on" and the cell itself is "on" at time n This results, for example in the sequence of cell states below. We assume that the cells extend infinitely far in all directions, with state 0 for those cells that are not displayed. n 1 n 2 n 3 n 4 n 5 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1
We will consider, however, even a simpler situation, that of one-dimensional cellular automata. The rules we use will all be "nearest neighbor" rules: The state of a cell at time n 1 will depend only on the values, at time n, of that cell and its neighbors on either side. At time n there are eight possible states for a given cell and its two nearest neighbors, written numerically as: 111 110 101 100 011 010 001 000 Note that the ordering is that of base 2 numbers, with 0 on the right and 7 on the left. These can be represented schematically with black and white boxes. Now in a given rule, corresponding to each of these 3-cell states, we must specify at time n 1 whether the middle cell on on or off. An example of such a specification, drawn from Wolfram s online examples, is Note that numerically, with white corresponding to 0 and black to 1, the rule can be written: 111 110 101 100 011 010 001 000 0 0 0 1 0 1 1 0 If we interpret the number below in base 2, we obtain the decimal number 16 4 2 22. This rule is given the number 22 in Wolfram s numbering system. In total, you can see that there are 2 8 256 possible such rules - there are eight values to be determined, each of which can be one of two values, giving 2 8, or you can simply note that the total number of distinct rules is the number of binary numbers that can be written with 8 digits, which is 2 8 256. Wolfram provides information on all of these so-called elementary rules at the web page http://atlas.wolfram.com/01/01/ Our next job is to program in MATLAB how to compute and display the evolution of a one-dimensional cellular automaton under these elementary rules. We will assume 1) We only use rules for which 000 at time n results in 0 for the middle cell at time n 1 2) At the initial time (say n 1), there are only a finite number of cells that are "on" We will use MATLAB to display the entire evolution of the cellular automaton, that is, we will display the record of all active cells as time goes on, with n 1 displayed as a row of cells at the top and we add another row of cells below each time we step forward in time from n to n 1. Here is a display of the first states of rule 22 applied to an intial state of. As time goes on (and we go down the screen) the active cells "spread out". 2
Corresponding to this picture is a matrix of zeros and 1 s of size 13x28. To start, we have a 1x4 matrix 1 0 1 1 for the initial state and then each time n increases by 1 we add one row and two columns so as to display all possible active cells. For the complete picture above we have added 12 rows and 24 columns to arrive at 13x28. Applying the rule itself is fairly simple - the annoying part is adjusting the size of the matrix and calculating the "boundary" cells in the next state. Here is our algorithm: At time n the matrix A (with n rows) holds the history of cell states. Row n of A, in MATLAB notation A(n,:), holds the current state. As an example, suppose the nth row of A is 1 0 1 1 0 1 0 0 1 0, displaying the states of 10 cells. This means that the infinite row of cells is in the state... 0 0 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0..., where the cells in A are outlined in brackets. At the next time step at most one additional cell to the left and right may be active. Set x A(n,:). We form the "padded" array [0 0 x 0 0] and we determine the values of the 12 cells corresponding to the original 10 cells in x and one additional cell on either side. In our example of rule 22 we have 1 0 1 1 0 1 0 0 1 0 x 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0 x 0 0 1 1 0 0 0 0 1 1 1 1 1 0 y where y is the next row to go into the matrix A. We expand A on either side with zero columns and insert y at the bottom by saying A [zeros(n,1) A zeros(n,1) ; y]. As for calculating y itself, we set k length(x) and then x [0 0 x 0 0]. The array y will have k 2 entries and x now has k 4 entries. Element y(i) is then calculated from the three values x(i : (i 2)). Applying the rule: The rule will be one of the inputs. It is specified as an array of 8 zeros and ones, defining the rule as discussed above. Now given an array of 3 0 s and 1 s, how do we figure out what the next value of the central cell will be? The key is to find the binary value corresponding to that array. If our array is a b c the binary value is 4a 2b c. Here are the individual values 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 Now if we look at how the rule is specified, 000, the binary value 0, corresponds to the 8th element of the rule array; 001, binary value 1, corresponds to the 7th value of the rule array. 3
Continuing, 101, binary value 5, corresponds to the 3rd element of the rule array. Do we see a pattern? Given the states a b c at time n, we calculate the binary value v 4a 2b c and then the state of the middle cell at time n 1 is given by rule(8-v). We begin by setting the colormap, and the initial state, the first row of A. Then we calculate the next row using the function named "update" and add it to the bottom of A. The image is displayed and then we pause briefly before going back to calculate the next state. 4
function wolfram(x0,rule,n) figure colormap gray(2) A x0; %staring array is set for n 1:N %N steps y update(a(n,:),rule); A [zeros(n,1) A zeros(n,1);y]; image(2-a); pause(.1) end The function "update" calculates the next state. The last row of A, here named x, is padded and then the next row of A is calculated using one of the rules. function y update(x,rule) n length(x) 2; y zeros(1,n) x [0 0 x 0 0]; %pad x for i 1:n %calculate y(i) s x(i:(i 2)); v 4*s(1) 2*s(2) s(3); y(i) rule(8-v); end 5