📄 mxanthus_swarm_opt.m
字号:
elseif Jmove==Js % best to move in S direction Pindexfront(:,i,j+1)=s; elseif Jmove==Jse % best to move in SE direction Pindexfront(:,i,j+1)=se; elseif Jmove==Je % best to move in E direction Pindexfront(:,i,j+1)=e; end end % Back of cell at NE location: if Pindexback(:,i,j)==ne Jmove=min([Jnw,Jw,Jsw,Js,Jse]); % Find the minimum cost of the three possible forward locations+left/right if Jmove==Jnw % best to move in NW direction Pindexfront(:,i,j+1)=nw; elseif Jmove==Jw % best to move in W direction Pindexfront(:,i,j+1)=w; elseif Jmove==Jsw % best to move in SW direction Pindexfront(:,i,j+1)=sw; elseif Jmove==Js % best to move in S direction Pindexfront(:,i,j+1)=s; elseif Jmove==Jse % best to move in SE direction Pindexfront(:,i,j+1)=se; end end % Back of cell at E location: if Pindexback(:,i,j)==e Jmove=min([Jn,Jnw,Jw,Jsw,Js]); % Find the minimum cost of the three possible forward locations+left/right if Jmove==Jn % best to move in N direction Pindexfront(:,i,j+1)=n; elseif Jmove==Jnw % best to move in NW direction Pindexfront(:,i,j+1)=nw; elseif Jmove==Jw % best to move in W direction Pindexfront(:,i,j+1)=w; elseif Jmove==Jsw % best to move in SW direction Pindexfront(:,i,j+1)=sw; elseif Jmove==Js % best to move in S direction Pindexfront(:,i,j+1)=s; end end % Back of cell at SE location: if Pindexback(:,i,j)==se Jmove=min([Jsw,Jw,Jnw,Jn,Jne]); % Find the minimum cost of the three possible forward locations+left/right if Jmove==Jsw % best to move in SW direction Pindexfront(:,i,j+1)=sw; elseif Jmove==Jw % best to move in W direction Pindexfront(:,i,j+1)=w; elseif Jmove==Jnw % best to move in NW direction Pindexfront(:,i,j+1)=nw; elseif Jmove==Jn % best to move in N direction Pindexfront(:,i,j+1)=n; elseif Jmove==Jne % best to move in NE direction Pindexfront(:,i,j+1)=ne; end end % Back of cell at S location: if Pindexback(:,i,j)==s Jmove=min([Jw,Jnw,Jn,Jne,Je]); % Find the minimum cost of the three possible forward locations+left/right if Jmove==Jw % best to move in W direction Pindexfront(:,i,j+1)=w; elseif Jmove==Jnw % best to move in NW direction Pindexfront(:,i,j+1)=nw; elseif Jmove==Jn % best to move in N direction Pindexfront(:,i,j+1)=n; elseif Jmove==Jne % best to move in NE direction Pindexfront(:,i,j+1)=ne; elseif Jmove==Je % best to move in E direction Pindexfront(:,i,j+1)=e; end end % Back of cell at SW location: if Pindexback(:,i,j)==sw Jmove=min([Jnw,Jn,Jne,Je,Jse]); % Find the minimum cost of the three possible forward locations+left/right if Jmove==Jnw % best to move in NW direction Pindexfront(:,i,j+1)=nw; elseif Jmove==Jn % best to move in N direction Pindexfront(:,i,j+1)=n; elseif Jmove==Jne % best to move in NE direction Pindexfront(:,i,j+1)=ne; elseif Jmove==Je % best to move in E direction Pindexfront(:,i,j+1)=e; elseif Jmove==Jse % best to move in SE direction Pindexfront(:,i,j+1)=se; end end % Back of cell at W location: if Pindexback(:,i,j)==w Jmove=min([Jn,Jne,Je,Jse,Js]); % Find the minimum cost of the three possible forward locations+left/right if Jmove==Jn % best to move in N direction Pindexfront(:,i,j+1)=n; elseif Jmove==Jne % best to move in NE direction Pindexfront(:,i,j+1)=ne; elseif Jmove==Je % best to move in E direction Pindexfront(:,i,j+1)=e; elseif Jmove==Jse % best to move in SE direction Pindexfront(:,i,j+1)=se; elseif Jmove==Js % best to move in S direction Pindexfront(:,i,j+1)=s; end end % Back of cell at NW location: if Pindexback(:,i,j)==nw Jmove=min([Jne,Je,Jse,Js,Jsw]); % Find the minimum cost of the three possible forward locations+left/right if Jmove==Jne % best to move in NE direction Pindexfront(:,i,j+1)=ne; elseif Jmove==Je % best to move in E direction Pindexfront(:,i,j+1)=e; elseif Jmove==Jse % best to move in SE direction Pindexfront(:,i,j+1)=se; elseif Jmove==Js % best to move in S direction Pindexfront(:,i,j+1)=s; elseif Jmove==Jsw % best to move in SW direction Pindexfront(:,i,j+1)=sw; end end %%%%%%%%%%%%%%%%%% % Next, for the move determined above update cell location, chemoattractant, and slime trails Pindexback(:,i,j+1)=Pindexfront(:,i,j); Pfront(:,i,j+1)=[xt(Pindexfront(1,i,j+1),1);xt(Pindexfront(2,i,j+1),2)]; Pback(:,i,j+1)=[xt(Pindexback(1,i,j+1),1);xt(Pindexback(2,i,j+1),2)]; % Lay down slime trail slimetrails(Pindexfront(2,i,j+1),Pindexfront(1,i,j+1))=min([slimetrails(Pindexfront(2,i,j+1),Pindexfront(1,i,j+1)),-slimedepth]); % Lay slime trail if not there slimetrails(Pindexback(2,i,j+1),Pindexback(1,i,j+1))=min([slimetrails(Pindexback(2,i,j+1),Pindexback(1,i,j+1)),-slimedepth]); % Lay slime trail if not there end % Go to next bacterium % Evaporate: slimetrails=slimeevaporationrate*slimetrails; % Evaporate slime % Save the following for plotting if j==Nc/2 slimetrailspart=slimetrails; end %---------------------------------end % j=1:Nc%---------------------------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Next, provide some plots of the results of the simulation.%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Plot bacteria locations, part wayfigure(3) clfif flag~=1 contour(xt(:,1),xt(:,2),nutrients,20) colormap(jet)endhold oncontour(xt(:,1),xt(:,2),slimetrailspart,5) % Put on slime trailcolormap(jet)plot(squeeze(Pfront(1,:,Nc/2)),squeeze(Pfront(2,:,Nc/2)),'kx')plot(squeeze(Pback(1,:,Nc/2)),squeeze(Pback(2,:,Nc/2)),'ko') xlabel('\theta_1');ylabel('\theta_2');title('Bacteria locations and slime trails, part way (x=front, o=back)')hold off%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Plot final locations:figure(4) clfif flag~=1 contour(xt(:,1),xt(:,2),nutrients,20) colormap(jet)endhold oncontour(xt(:,1),xt(:,2),slimetrails,5) % Put on slime trailcolormap(jet)plot(squeeze(Pfront(1,:,Nc)),squeeze(Pfront(2,:,Nc)),'kx')plot(squeeze(Pback(1,:,Nc)),squeeze(Pback(2,:,Nc)),'ko') xlabel('\theta_1');ylabel('\theta_2');title('Bacteria locations and slime trails, final (x=front, o=back)')hold off%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Next, show a movie of the movements of the bacteria positions%figure(5) %clf%M = moviein(Nc);% for j=1:Nc;% xlabel('\theta_1')% ylabel('\theta_2')% title('Bacteria movements (x marks food location)')% plot(squeeze(Pfront(1,:,j)),squeeze(Pfront(2,:,j)),'*')% axis([0,30,0,30])% hold on% foodlocation=plot(8,25,'rx');% set(foodlocation,'MarkerSize',15)% set(foodlocation,'LineWidth',3)% hold off% M(:,j)=getframe;% end; %movie(M,0)%save bacteria_swarm%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% End of program%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -