主程序:NaSch_3.m程序代码s
% 单车道 最大速度3个元胞 开口边界条件 加速 减速 随机慢化
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=100; %数据初始化
z=zeros(1,n); %元胞个数
z=roadstart(z,5); %道路状态初始化,路段上随机分布5辆
cells=z;
vmax=3; %最大速度
v=speedstart(cells,vmax); %速度初始化
x=1; %记录速度和车辆位置
memor_cells=zeros(3600,n);
memor_v=zeros(3600,n);
imh=imshow(cells); %初始化图像白色有车,黑色空元胞
set(imh, 'erasemode', 'none')
axis equal
axis tight
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)
%边界条件处理,搜素首末车,控制进出,使用开口条件
a=searchleadcar(cells);
b=searchlastcar(cells);
[cells,v]=border_control(cells,a,b,v,vmax);
i=searchleadcar(cells); %搜索首车位置
for j=1:i
if i-j+1==n