《Cellular Automata in Matlab.docx》由会员分享,可在线阅读,更多相关《Cellular Automata in Matlab.docx(13页珍藏版)》请在第壹文秘上搜索。
1、CellularAutomatainMatlabIntroductionCellularAutomata(CA)areaschemeforcomputingusinglocalrulesandlocalcommunication.TypicallyaCAisdefinedonagrid,witheachpointonthegridrepresentingacellwithafinitenumberofstates.Atransitionruleisappliedtoeachcellsimultaneously.Typicaltransitionrulesdependonthestateofth
2、ecellandits(4or8)nearestneighbors,althoughotherneighborhoodsareused.CAshaveapplicationsinparallelcomputingresearch,physicalsimulations,andbiologicalsimulations.ThispagewillconsiderhowtowriteefficientmatlabcodetoimplementCAsandlookatsomeinterestingrules.codemakesuseofMatlab,sveryflexibleindexingtospe
3、cifythenearestneighbors. x=2:n-l; y-2:n-l; sum(x,y)=cells(x,y-l)+cells(x,y+l)+. cells(x-l,y)+cells(x+l,y)+. cells(x-l,y-l)+cells(-l,y+l)+ cells(x+l,y-l)+cells(x+l,y+l); cells=(sum=3)(sum=2&cells); AddingasimpleGUIiseasy.Inthisexamplethreebuttonsandatextfieldwereimplmented.Thethreebuttonsallowauserto
4、Run,Stop,orQuit.Thetextfielddisplaysthenumberofsimulationstepsexecuted. %buildtheGUI %definetheplotbutton plotbutton=uicontrol(,style,pushbutton,. ,string,Run, ,fontsize,12, ,position,100,400,50,20,. ,callback,run=l;,); %definethestopbutton erasebutton-uicontrol(style,pushbutton1,. ,string,Stop, ,fo
5、ntsize,12, ,position,200,400,50,20,. ,callback,freeze=l;,); %definetheQuitbutton quitbutton=uicontrol(style,pushbutton,. ,string,Quit, ,fontsize,12, ,position,300,400,50,20, ,callback*,stop=l;close/); number=uicontrol(,style,text, ,string*,1, ,fontsize,12, ,position,20,400,50,20);Afterthecontrols(an
6、dCA)areinitialized,theprogramdropsintoaloopwhichteststhestateofthevariableswhicharesetinthecallbackfunctionsofeachbutton.Forthemoment,justlookatthenestedstructureofthewhileloopandifstatements.TheprogramloopsuntiltheQuitbuttonispushed.Theothertwobuttonscauseanifstatementtoexecutewhenpushed.stop=0;%wa
7、itforaquitbuttonpushrun=0;%waitforadrawfreeze=0;%waitforafreezewhile(StoP=O)if(run=l)%nearestneighborsumsum(x,y)二cells(x,y-l)+cells(x,y+l)+.cells(-l,y)+cells(x+l,y)+.cells(-l,y-l)+cells(-l,y+l)+.cells(3:n,y-l)+cells(x+l,y+l);%TheCArulecells=(SUnI=3)(SUm=2&cells);%drawthenewimageset(imh,cdata,cat(3,c
8、ells,z,z)%updatethestepnumberdiaplaystepnumber=1str2num(get(number,string,);set(number,string,num2str(stepnumber)endif(freezel)run=0;freeze=0;enddrawnow%needthisintheloopforcontrolstoworkendExamples1. ConwaySlife.Theruleis:oSumthe8nearestneighborsoIfthesum=2thenthestatedoesnotchangeoIfthesum=3thenth
9、estate=loOtherwisethestate=OThecode:x=2:n-l;y-2:n-l;%nearestneighborsumsum(x,y)=cells(x,y-l)+cells(x,y+l)+.cells(x-l,y)+cells(x+l,y)+.cells(x-l,y-l)+cells(x-l,y+l)+.cells(3:n,y-l)+cells(x+l,y+l);%TheCArulecells=(sum=3)I(sum=2&cells);2. SUrfaCeTenSionTheruleis:oSumthe8nearestneighborsandthecellitself
10、oIfthesum4orsum=5thenthestate=OoOtherwisethestate-1Thecode:x=2:n-l;y=2:n-l;sum(x,y)=cells(x,y-l)+cells(x,y+l)+.cells(-l,y)+cells(x+l,y)+.cells(-l,y-l)+cells(-l,y+l)+cells(3:n,y-l)+cells(xl,y+l)+.cells(x,y);%TheCArulecells=(sum0(atleastoneneighbor)andtherthreshold,andthecellhasneverhadaneighborthence
11、ll=l.oIfthesum0setthevisitedflagforthecel1torecordthatthecellhasanonzeroneighbor.Theupdatecode:sum(2:a-l,2:b-l)=cells(2:a-l,1:b-2)+cells(2:a-l,3:b)+.cells(1:a-2,2:bT)+cells(3:a,2:b-l)+cells(1:a-2,1:b-2)+cells(1:a-2,3:b)+cells(3:a,1:b-2)+cells(3:a,3:b);pick=rand(a,b);cells=cells(sum=l)&(pick=threshol
12、d)&(visit=0)visit-(sum=l);Thevariablesaandbarethesizeoftheimage.Theinitalimageisdeterminedbygraphicsoperations.Thefollowingstatementssetupaxesofafixedsize,writetextintotheaxes,thengrabthecontentsoftheaxesandputthembackintoanarrayusingthegetframefunction.ax-axes(,units,pixels,position,11500400,color,
13、k,);text(,units,pixels,position,50,255,0,string,BioNB,color,w,fontname,helvetica,fontsize,100)text(,units,pixels,position,120,120,0,.,string,44,color,w,fontname,helvetica,fontsize,100)initial-getframe(gca);a,b,c-size(initial.cdata);z=zeros(a,b);cells=double(initial,cdata(:,:,1)255);visit=Z;sum=z;Aft
14、erafewdozentimesteps(startingwiththeBioNB441image)wegetthefollowingimage.Clickonittoseeafullsizeimage.4. EXCitabIeInedia(BZreactionorheart)Therule:oCellscanbein10differentstates.State0isresting.States1-5areactive,andstates6-9arerefractory.oCountthe8nearestneighborsofeachcellwhichareinoneoftheactivestates.oIfthesumisgreaterorequalto3(atleastthreeactiveneighbors)thencell=l.oStates1to9occurstepwisewithnomoreinput.I