# Modeling an intersection

1. Feb 24, 2013

### gryphon1221

Hi, I am trying to model an intersection using different rules but I am having a hard time getting the thing to work in the simplest stage. I know there is probably an easier way to do this but I am trying to do it this way:

clear
grid=zeros(10,10); %grid creation
grid2=zeros(10,10);
grid3=zeros(10,10);
grid(1:4,1:4)=2; %empte spaces representing city blocks
grid(7:10,1:4)=2;
grid(1:4,7:10)=2;
grid(7:10,7:10)=2;

grid(6,2)=1; %initial grid coords for cars
grid(6,4)=1;
grid(7,6)=1;
grid(5,7)=1;
grid(5,9)=1;
grid(4,5)=1;
grid(2,5)=1;
grid(9,6)=1;

tfinal=100;
for t=1:10
random=randi(10,1);
if grid(6,10)==1;
grid2(6,10)=0;
end
if grid(6,9)==1;
grid2(6,10)=1 & grid3(6,9)==-1;
end
if grid(6,8)==1;
grid2(6,9)=1 & grid3(6,8)==-1;
end
if grid(6,7)==1;
grid2(6,8)=1 & grid3(6,7)==-1;
end
if grid(6,6)==1;
grid2(6,7)=1 & grid3(6,6)==-1;
end
if grid(6,5)==1;
grid2(6,6)=1 & grid3(6,5)==-1;
end
if (grid(6,4)==1) && 0<t<=(tfinal/10)
grid2(6,5)=1 & grid3(6,4)==-1;
end
if grid(6,3)==1;
grid2(6,4)=1 & grid3(6,3)==-1;
end
if grid(6,2)==1;
grid2(6,3)=1 & grid3(6,2)==-1;
end
if grid(6,1)==1;
grid2(6,2)=1 & grid3(6,1)==-1;
elseif grid(6,1)==0 & random>=5;
grid2(6,1)=1;
end
grid2=grid3+grid2;
grid=grid2;
end

This is just the first of 4 aspects of the intersection node. I want the cars to move ahead in each column, except a time limitation on column 5 representing the signal. This is my first time using & for anything in matlab, and I have a feeling not understanding its use is causing me problems. I have been trying for a while and I am just plain stuck now. Anyone have any suggestions?

2. Feb 25, 2013

### gryphon1221

I am taking a different approach but still cant get it to work.

For the initial movement I got it, but after a time delay I can't seem to figure it out. I want the vector to move when 12<t<=22, but when I type the inequality into a conditional it seems to not be working. Here is the code I am using for the second movement right now (not working)

for t=1:22
if ((tfinal/10)+1)<t<=((2*tfinal/10)+2)
left_traffic_post_inter=left_traffic(1:6);
elseif t>((2*tfinal/10)+2)
left_traffic_post_inter=circshift(left_traffic_post_inter,[0 -1]);
end

if left_traffic_post_inter(1)==1
left_traffic_post_inter(1)=0;
end
left_traffic=[left_traffic_post_inter(1:6) left_traffic(7:10)];
end

The second part of the code is when the intersection switches again and the cars stop moving before the intersection, but finish moving if they are in or after the intersection. Thanks for any insight.