www.pudn.com > model1-3.0.rar > model2.m, change:2014-02-09,size:3467b

```% [ flux,biand,acc,distant ] = model2( 120, 0.5, 0.4, 3, 1,30 ,5, 0.1, 1, 2, 1, 1)
function [ flux,biand,acc,distant ] = model2( time, density1, density2, length_car1, length_car2, length_road , maxspeed, possibility, acc1,acc2,min_dis1,min_dis2 )

t=1;    %时间步长
totaltime=time;  %总模拟时间s
rou1=density1;%密度
rou2=density2;
l(1)=length_car1;    %车长m
l(2)=length_car2;
vmax=maxspeed; %车道最大速度m/s
p=possibility;  %随机减速概率
a(2)=acc2;   %加速度（简化为定值）
a(1)=acc1;   %两种不同车型
dis(2)=min_dis2; %要求不同超车车头距
dis(1)=min_dis1;

flux=[0,0] ;
biand=[0,0] ;
acc=[0,0];
distant=[0,0];

temp=randperm(2*r);
car=cell(1,n1+n2);
for i=1:n1
car{i}.m=1;         %different car model
car{i}.lane=1;      %初始在慢车道上
car{i}.x=temp(1,i);  %初始车头位置
car{i}.v=randi(vmax);%初始速度（取整）
end
for i=n1+1:n1+n2
car{i}.m=2;         %different car model
car{i}.lane=3;      %初始在快车道上
car{i}.x=temp(1,i);  %初始车头位置
car{i}.v=randi(vmax);%初始速度（取整）
end
color(1,r)=uint8(0);%慢车道
color(3,r)=uint8(0);%快车道

for i=1:n1+n2
color(car{i}.lane,mod(car{i}.x-l(car{i}.m):car{i}.x-1,r)+1)=car{i}.m;
end
stepnumber=0;

while (stepnumber< (totaltime/t) )
%         RGBmap = [1 1 1; 0 0 1; 1 0 0]; %0白（路），1蓝（coach）,2red(car)
%         colormap(RGBmap);
%         image(color);
%         axis([0.5 r+0.5 -r/2 r/2+5]);
%         pause(0.2);

for j=1:n1+n2
%distance
for k=1:r
if(color(car{j}.lane,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车
break;
end
end
distant(car{j}.m)=distant(car{j}.m)+k;%平均距离格子数

if(color(car{j}.lane,mod(car{j}.x:car{j}.x+3-1,r)+1)==0)%前面3格里没车
car{j}.v=min(car{j}.v+a(car{i}.m),vmax);%加速过程
acc(car{j}.m)=acc(car{j}.m)+1;
else
if(color(4-car{j}.lane,mod(car{j}.x-1:car{j}.x+3-1,r)+1)==0) %左/右及前3格没车
if(color(car{j}.lane,mod(car{j}.x:car{j}.x+dis(car{j}.m)-1,r)+1)==0)    %前面dis格里没车
car{j}.lane=4-car{j}.lane;  %变道
biand(car{j}.m)=biand(car{j}.m)+1;
end
end
end

for k=1:r    %保证安全，不撞车
if(color(car{j}.lane,mod(car{j}.x+k-1,r)+1)~=0)  %寻找前一辆车
%                    disp(car{j});
%                    disp(k);
break;
end
end
car{j}.v=min(car{j}.v,k-1); %安全刹车

if(rand(1)<=p)
car{j}.v=max(car{j}.v-1,0);%随机慢化过程
end

if (car{j}.x+car{j}.v > r)
flux(car{j}.m)=flux(car{j}.m)+1;
end
car{j}.x=mod(car{j}.x+car{j}.v-1,r)+1;%位置更新,循环边界条件

end
color(1,1:r)=0;
color(3,1:r)=0;
for i=1:n1+n2
color(car{i}.lane,mod(car{i}.x-l(car{i}.m):car{i}.x-1,r)+1)=car{i}.m;
end
stepnumber = stepnumber+1 ;

end

distant(1)=distant(1)/n1/(totaltime/t);
distant(2)=distant(2)/n2/(totaltime/t);
```