Modeling an Intersection with Rules for Traffic Flow in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter gryphon1221
  • Start date Start date
  • Tags Tags
    Intersection Modeling
Click For Summary
SUMMARY

This discussion focuses on modeling a traffic intersection in MATLAB using grid-based simulation techniques. The user employs a 10x10 grid to represent city blocks and car movements, utilizing MATLAB's logical operators and random number generation. Key issues include the correct implementation of conditional statements for traffic flow and the use of the logical AND operator (&) in MATLAB. The user seeks advice on managing time delays and conditional movements for vehicles at the intersection.

PREREQUISITES
  • Understanding of MATLAB programming, specifically logical operators.
  • Familiarity with grid-based modeling techniques.
  • Knowledge of conditional statements and loops in MATLAB.
  • Basic concepts of traffic flow simulation.
NEXT STEPS
  • Research MATLAB logical operators and their correct usage.
  • Explore MATLAB's 'circshift' function for managing array movements.
  • Learn about implementing time-based conditions in MATLAB simulations.
  • Investigate advanced traffic simulation techniques, such as using state machines.
USEFUL FOR

This discussion is beneficial for MATLAB programmers, traffic simulation developers, and anyone interested in modeling complex systems involving time-dependent behaviors and logical conditions.

gryphon1221
Messages
9
Reaction score
0
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?
 
Physics news on Phys.org
I am taking a different approach but still can't 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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
5K