%Conway's life with GUI clf %clear all %============================================= %build the GUI %define the plot button plotbutton=uicontrol('style','pushbutton',... 'string','Run', ... 'fontsize',12, ... 'position',[100,400,50,20], ... 'callback', 'run=1;'); %define the stop button erasebutton=uicontrol('style','pushbutton',... 'string','Stop', ... 'fontsize',12, ... 'position',[200,400,50,20], ... 'callback','freeze=1;'); %define the Quit button quitbutton=uicontrol('style','pushbutton',... 'string','Quit', ... 'fontsize',12, ... 'position',[300,400,50,20], ... 'callback','stop=1;close;'); number = uicontrol('style','text', ... 'string','1', ... 'fontsize',12, ... 'position',[20,400,50,20]); %============================================= %CA setup n=64; global gd % probability of moving up or down based on gravity %global gn global P % probability of breaking with vacant space opposite global J % probability of joining with vacant space between global N % probability of doing nothing. all other events sum to probability of (1-N) % This is like a temperature effect. global emptynum global waternum global oilnum global solutenum N(1) = 0.5; N(2) = 0.5; gd(1,1) = 0; % 1st index for lower cell, 2nd index for higher cell gd(2,2) = 0; gd(1,2) = 0.20; gd(2,1) = 0.30; %gn(1,1) = 0; %gn(2,2) = 0; %gn(1,2) = 0.25; %gn(2,1) = 0.20; P(1,1) = 0.3; P(2,2) = 0.5; P(1,2) = 0.9; P(2,1) = 0.9; J(1,1) = 1.0; J(2,2) = 0.5; J(1,2) = 0.25; J(2,1) = 0.25; N(3) = 0.5; gd(3,3) = 0; gd(1,3) = 0; gd(2,3) = 0.30; gd(3,1) = 0; gd(3,2) = 0.20; %gn(3,3) = 0; %gn(1,3) = 0.25; %gn(2,3) = 0.20; %gn(3,1) = 0.25; %gn(3,2) = 0.20; P(1,3) = 0.3; P(3,1) = 0.3; P(2,3) = 0.9; P(3,2) = 0.9; P(3,3) = 0.3; J(1,3) = 1.0; J(3,1) = 1.0; J(2,3) = 0.25; J(3,2) = 0.25; J(3,3) = 1.0; emptynum = 4; waternum = 1; oilnum = 2; solutenum = 3; %initialize the arrays z = zeros(n,n); %cells = z; cells1 = z; %cells2 = z; %sum1 = z; %set a few cells to one %cells(n/2,.25*n:.75*n) = 1; %cells(.25*n:.75*n,n/2) = 1; %cells(.5*n-1,.5*n-1)=1; %cells(.5*n-2,.5*n-2)=1; %cells(.5*n-3,.5*n-3)=1; % fill in rest of cells with water (blue(1) 'a') or oil (green(2) 'b') for i = 1:n for j = 1:n % fill in white, or empty squares (4) if rand < 0.3 cells1(i,j) = emptynum; end if cells1(i,j) == 0 if rand < 0.5 cells1(i,j) = waternum; else cells1(i,j) = oilnum; end end % make a few cells the solute (red(3) 'c') if rand < 0.03 cells1(i,j) = solutenum; end end end %cells1(1,:) = 4; %cells1(n,:) = 4; %cells1(:,1) = 4; %cells1(:,n) = 4; mapgb = [0 0 1; 0 1 0; 1 0 0; 1 1 1;]; colormap(mapgb) %cells2 = 1 - cells1; %how long for each case to stability or simple oscillators %build an image and display it imh = image(cells1); set(imh, 'erasemode', 'none') axis equal axis tight %index definition for cell update x = 1:n; y = 1:n; %Main event loop stop= 0; %wait for a quit button push run = 0; %wait for a draw freeze = 0; %wait for a freeze while (stop==0) if (run==1) %nearest neighbor sum % cells = conwayrule(cells,x,y,sum1); cells1 = separate2rule(cells1,x,y); %draw the new image set(imh, 'cdata', cells1) %update the step number diaplay stepnumber = 1 + str2num(get(number,'string')); set(number,'string',num2str(stepnumber)) end if (freeze==1) run = 0; freeze = 0; end drawnow %need this in the loop for controls to work end