cellular automata: exploring applications erik aguilar amelia yzaguirre amy femal
TRANSCRIPT
Rules for the Game of Life•Any live cell with fewer than 2 live neighbors dies
•Any live cell with more than 3 neighbors dies
•Any live cell with 2 or 3 live neighbors lives on to the next generation
•Any dead cell with exactly 3 live neighbors becomes a live cell
Writing the code
%First we must create an n x n matrix of zeros.n=75; z=zeros(n,n); %Produces random "0" and "1" throughout matrixcells = (rand(n,n))<.12; %Creates an image using the matriximh = image(cat(3,cells,z,z)); %When the image is replaced, the old image is not erased firstset(imh, 'erasemode', 'none')
x=(2:n-1); %creates array x which is used for positiony=(2:n-1); %creates array y which is used for position %creates matrix for summationssum(x,y)=zeros(n-2,n-2);
a=0;while a==0; %equations for the corner cells to check for infected %neighbors sum(1,1)=cells(1,2)+cells(2,1)+cells(2,2); sum(1,n)=cells(2,n)+cells(2,n-1)+cells(1,n-1); sum(n,1)=cells(n,2)+cells(n-1,1)+cells(n-1,2); sum(n,n)=cells(n,n-1)+cells(n-1,n)+cells(n-1,n-1); %equations for the edge rows/columns to check for infected %neighbors sum(1,y)=cells(1,y-1)+cells(1,y+1)+cells(2,y)+cells(2,y+1)+... cells(2,y-1); sum(n,y)=cells(n,y+1)+cells(n,y-1)+cells(n-1,y)+cells(n-1,y+1)+... cells(n-1,y-1); sum(x,1)=cells(x-1,1)+cells(x+1,1)+cells(x,2)+cells(x+1,2)+... cells(x-1,2); sum(x,n)=cells(x-1,n)+cells(x+1,n)+cells(x,n-1)+cells(x+1,n-1)+... cells(x-1,n-1);
%equations for the interior cells to check for infected %neighbors sum(x,y)=cells(x,y+1)+cells(x,y-1)+cells(x+1,y)+cells(x-1,y)+... cells(x+1,y+1)+cells(x+1,y-1)+cells(x-1,y+1)+cells(x-1,y-1); %updating the cells matrix cells = (sum(1:n,1:n)==3)|(sum(1:n,1:n)==2 & cells); %draw the new image set(imh, 'CData', cat(3,cells,z,z)) drawnow %displaying the updated image waitforbuttonpress end
Excitable Mediacan be observed in the BZ reaction
Belousov-Zhabotinsky reaction
What is the BZ reaction?It is a chemical reaction caused by the mixture of
Sulfuric Acid
Sodium Bromate
Malonic Acid
Sodium Bromide
Phenanthroline Ferrous Sulfate
Triton X-100 Surfactant
Rules for the BZ ReactionCells can be in 10 different states. State 0 = resting States 1 – 5 = active States 6 – 9 = refractory
Like LIFE, each cell of the BZ reaction is dependent on its 8 surrounding neighbors. • If 3 or more neighbors are active, cell = 1• A cell in State 1 will change to State 2. • A cell in State 2 will change to State 3 and so on.• A cell in State 9 will change to State 0.
Code for the BZ Reaction
clear all n=200; %size M=zeros(n); %this will give us an n by n matrixgrid=M; %the grid will be made up of the n by n matrixgrid=(rand(n))<.06; sum=M;
bz=image(cat(3,grid,M,M)); x=[2:n-1];y=[2:n-1];axis tight
t = 6; % when t is in active statefor i=1:1000 %duration of loop sum(x,y) = ((grid(x,y-1)>0)&(grid(x,y-1)<t)) + ... ((grid(x,y+1)>0)&(grid(x,y+1)<t)) + ... ((grid(x-1, y)>0)&(grid(x-1, y)<t)) + ... ((grid(x+1,y)>0)&(grid(x+1,y)<t)) + ... ((grid(x-1,y-1)>0)&(grid(x-1,y-1)<t)) + ... ((grid(x-1,y+1)>0)&(grid(x-1,y+1)<t)) + ... ((grid(x+1,y-1)>0)&(grid(x+1,y-1)<t)) + ... ((grid(x+1,y+1)>0)&(grid(x+1,y+1)<t)); %the sum of each cell in active state of the 8 surrounding neighbors grid = ((grid==0) & (sum>=3)) + 2*(grid==1) + 3*(grid==2) + ... 4*(grid==3) + 5*(grid==4) + 6*(grid==5) +... 7*(grid==6) + 8*(grid==7) + 9*(grid==8) +... 0*(grid==9); %when state=1, next state=2... set(bz,'cdata', cat(3,M,grid/10,M) ) %bz is the image, cdata contains %data array drawnow %creates imageend
Lattice Gas AutomataRulesCells have 2 states 0 = empty 1 = moving gas particle
Each cell has 3 neighbors for a given time step where a block rule is applied to a 2 x 2 block of cells.
Odd Odd
Odd Cell Even
Even Even
Code for Lattice Gas Automata
%Cellular Automata model of gas particles in a box with a partition%This will make use of a Margolus neighborhood to create motion of an HPP%(Hardy, Pazzis, Pomeau) lattice gas... Curious if we meet Gibbs' paradox!
clear allclf %clears any frames being used.
%--------------We must first create our grid----------%These variables will be used to define the dimension of the matrix in our%gridnx=52; %must be divisible by 4, since each cell will be divided into groups of fourny=100;z=zeros(nx,ny); %Creates an nx by ny matrix of zeros called zo=ones(nx,ny); %Creates an nx by ny matrix of ones called o
%Initialize each of the matrices to be used latercells = z ; cellsNew = z; ground = z ; diag1 = z; diag2 = z; and12 = z;or12 = z; sums = z; orsum = z;
%create the boxground(1:nx,ny-3)=1 ; % right ground lineground(1:nx,3)=1 ; % left ground lineground(nx/4:nx/2-2,ny/2)=1; % the hole in the middle of the partitionground(nx/2+2:nx,ny/2)=1; %the hole in the middle of the partitionground(nx/4, 1:ny) = 1; %top lineground(3*nx/4, 1:ny) = 1; %bottom line
%We now want to "fill" the left side of the container with "gas particles"r = rand(nx,ny);cells(nx/4+1:3*nx/4-1, 4:ny/2-1) = r(nx/4+1:3*nx/4-1, 4:ny/2-1)<0.3;
%Define the image of the gas particles in the container!imh = image(cat(3,z,cells,ground));set(imh, 'erasemode', 'none')axis equalaxis tight
%This is where we define the motion of the particles
for i=1:1000 p=mod(i,2); %Margolus neighborhood defined %upper left cell update xind = [1+p:2:nx-2+p]; yind = [1+p:2:ny-2+p]; %See if exactly one diagonal is ones %We can only have one of the following to hold: diag1(xind,yind) = (cells(xind,yind)==1) & (cells(xind+1,yind+1)==1) & ... (cells(xind+1,yind)==0) & (cells(xind,yind+1)==0); diag2(xind,yind) = (cells(xind+1,yind)==1) & (cells(xind,yind+1)==1) & ... (cells(xind,yind)==0) & (cells(xind+1,yind+1)==0);
%This gives the diagonals both not occupied by two particles andboth(xind,yind) = (diag1(xind,yind)==0) & (diag2(xind,yind)==0); %This gives one diagonal occupied by two particles orone(xind,yind) = diag1(xind,yind) | diag2(xind,yind); %For a given gas particle, check if it is near the boundary sums(xind,yind) = ground(xind,yind) | ground(xind+1,yind) | ... ground(xind,yind+1) | ground(xind+1,yind+1) ; %Rules: %If (no walls) and (diagonals are both empty) %then there are no particles to swap, so the block stays the same cellsNew(xind,yind) = ... (andboth(xind,yind) & ~sums(xind,yind) & cells(xind+1,yind+1)) + ... (orone(xind,yind) & ~sums(xind,yind) & cells(xind,yind+1)) + ... (sums(xind,yind) & cells(xind,yind));
cellsNew(xind+1,yind) = (andboth(xind,yind) & ~sums(xind,yind) & cells(xind,yind+1)) + (orone(xind,yind) & ~sums(xind,yind) & cells(xind,yind))+ ... (sums(xind,yind) & cells(xind+1,yind)); %If (no walls) and (only one diagonal occupied) %then this is representative of a collision--- treat as though the %particles hit and deflect each other at 90 degrees, i.e. one diagonal %is converted to the other on the time step. cellsNew(xind,yind+1) = ... (andboth(xind,yind) & ~sums(xind,yind) & cells(xind+1,yind)) + ... (orone(xind,yind) & ~sums(xind,yind) & cells(xind+1,yind+1))+ ... (sums(xind,yind) & cells(xind,yind+1)); %If (wall) %then the cell stays the same in the block (causes a reflection) cellsNew(xind+1,yind+1) = ... (andboth(xind,yind) & ~sums(xind,yind) & cells(xind,yind)) + ... (orone(xind,yind) & ~sums(xind,yind) & cells(xind+1,yind))+ ... (sums(xind,yind) & cells(xind+1,yind+1));