Cellular Automata in Matlab.docx
《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.oCountthe8nearestneighborsofeachcellwhichareinoneoftheactives
15、tates.oIfthesumisgreaterorequalto3(atleastthreeactiveneighbors)thencell=l.oStates1to9occurstepwisewithnomoreinput.Ifstate=lthenthenextstate=2.Ifstate=2thenthenextstate=3,andsimilarlyofallthestatesupto9.Ifstate=9thenthenextstate=0andthecellisbackatrest.Theupdatecode:sum(x,y)=(cells(x,y-l)0)(cells(x,y
16、-l)0)&(cells(x,y+l)0)(cells(-l,y)0)&(cells(x+l,y)0)&(cells(-l,y-l)0)(cells(-l,y+l)0)&(cells(x+l,y-l)0)&(cells(x+l,y+l)=tl)+.2*(CeIIS=I)+.3*(CelIS=2)+4*(CeIIS=3)+.5*(CeIIS=4)+.6*(cells=5)+.7*(CeIIS=6)+.8*(CeIIS=7)+.9*(CeIIS=8)+.0*(CelIS=9);AnimagefromthisCAafterspiralwavesdevelopisshownbelow.5. FOreS
17、tFireTherule:oCellscanbein3differentstates.State=Oisempty,state=lisburningandstate=2isforest.oIfoneormoreofthe4neighborsifacellisburninganditisforest(state-2)thenthenewstateisburning(state-1).oThereisalowprobablity(say0.000005)ofaforestcell(state=2)startingtoburnonitsown(fromlightning).ocellwhichisb
18、urning(state=l)becomesempty(state=0).oThereisalowprobabi1ity(say,0.01)ofanemptycellbecomingforesttosimulategrowth.oThearrayisconsideredtobetoroidlyconnected,sothatfirewhichburnstoleftsidewillstartfiresontheright.Thetopandbottomaresimilarlyconnected.Theupdatecode:sum=(veg(l:n,n1:n-l)=l)+(veg(l:n,2:n1
19、)=1)+(veg(nl:n-l,l:n)=l)+(veg(2:n1,l:n)=l);veg=.2*(veg=2)-(veg=2)&(sum0(rand(n,n)Plightning)+.2*(veg=0)&rand(n,n)Pgrowth);Notethatthetoroidalconnectionisimplementedbytheorderingofsubscripts.6. GaSdynamicsThisCA(andthenexttwo)areusedtocomputemotionofparticles.Thisapplicationrequiresadifferentkindofne
20、ighborhood.Theneighborhoodofacellvarysoneachtimestepsothatmotioninacertaindirectionwillcontinueinthesamedirection.Inotherwords,theruleconservesmomentum,whichisthebasisofdynamicalcalculations.TheneighborhoodusediscalledaMargolisneighborhoodandconsistsofoverlapping2x2blocksofcells.Inthefollowingdiagra
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Cellular Automata in Matlab
链接地址:https://www.desk33.com/p-1407052.html