### 【元胞自动机】基于matlab元胞自动机双车道交通流模型含靠右行驶【含Matlab源码 231期】

MATLAB 物理模拟 自定义 多点 时空图

## 一、简介

figure 1:wolfram的184号规则

NaSch规则:

(1)加速，

(2)减速,

(3)以概率p随机慢化速度,

(4)行进，

figure 2:Density=0.15,Vmax=5

figure 2是元胞长度为1000，时间步为500时候得出的时空图。这里还有相应的包括流量密度图和速度密度图等基本图也可以得出来。

STNS规则:

Keep-Right Rule:

## 二、源代码

``````%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A Two-Lane Cellular Automaton Traffic Flow Model with the Keep-Right Rule
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
close all;
B=3; % The number of the lanes
plazalength=100; % The length of the simulation highways
h=NaN; % The handle of the image
[plaza,v]=create_plaza(B,plazalength);
h=show_plaza(plaza,h,0.1);
iterations=1000; % Number of iterations
probc=0.1; % Density of the cars
probv=[0.1 1]; % Density of two kinds of cars
probslow=0.3; % The probability of random slow
Dsafe=1; % The safe gap distance for the car to change the lane
VTypes=[1,2]; % Maximum speed of two different cars
[plaza,v,vmax]=new_cars(plaza,v,probc,probv,VTypes);% Generate cars on the lane
size(find(plaza==1))
PLAZA=rot90(plaza,2);
h=show_plaza(PLAZA,h,0.1);
for t=1:iterations;
size(find(plaza==1))
PLAZA=rot90(plaza,2);
h=show_plaza(PLAZA,h,0.1);
[v,gap,LUP,LDOWN]=para_count(plaza,v,vmax);
[plaza,v,vmax]=switch_lane(plaza,v,vmax,gap,LUP,LDOWN);
[plaza,v,vmax]=random_slow(plaza,v,vmax,probslow);
[plaza,v,vmax]=move_forward(plaza,v,vmax);
end
```

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
```