MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by George on 30 Nov 2012

I have been trying to do this for two days and have a cluster f of variables which in the end doesn't work. I also need a separate variable to count up all the time passed??

this is what i have it just keeps going up and doesn't reset at 60 seconds

clc clear n=7200; rlight=35; glight=60-rlight; x=zeros(n,1); greencars= zeros(n,1); inqueue=zeros(n,1); for i=1:n; while x<=floor(7140) x=(.5+4.5*rand)+x; if x<glight greencars(i,1)=greencars(i,1)+1; elseif x>=glight & x<=60 inqueue(i,1)=inqueue(i,1)+1; end end end gcars=sum(greencars) inqueue=sum(inqueue) wait=inqueue; line=wait-1; sec_cross=3.2+line*1.4 go=glight-sec_cross max(x)

*No products are associated with this question.*

Answer by Jan Simon on 30 Nov 2012

Perhaps this helps:

T = 0.5 + rand(1,1e6) * 4.0; % Times accumT = cumsum(t); % Accumulated times phaseT = rem(accumT, 60); % Green phases phases = find([true, diff(phaseT <= 0)); % Indices of green phases

The duration of the red phase does not matter, as far as I can see. It can be added at the `phases` to the timings without touching the rest of the calculations.

## 9 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114462

If I understand your question right you are going to need two loops

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114463

I need to count how many cars that go through the light during green each time, then hold in queue the cars at the red light till the next cycle, i need to do this for two hours with a short code

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114469

You need to know the red light timing as well, as during that time you need to accumulate cars. The timing of those queued cars through the next green light would not be random. Additional cars might arrive while previously-queued cars have not yet cleared the green light.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114471

you right! What about how to get the cycle to 60 seconds intervals do you know? This is basically passing 60 then going up till the end of loop

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114472

Why are you creating x as a vector, but always using

allof it in statements right until the final max(x) ?At one point you compare x to 7140, and then a couple of statements later you compare it to glight and to 60. Is x intended to be elapsed time since the beginning of the simulation, or time since the start of the last cycle?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114473

I already have the first minute or 60 seconds done and working as it is different then all the other minutes because there is initially no queue... What i am trying to do but do not know how is have a variable that can keep track of the time elapsed to tell the loop to stop, and have another variable keep track of the random numbers that cycle from 0:60. Also solve the problem you pointed out

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114474

The random numbers do not cycle from 0:60. The random numbers are 0.5 to 5.

I suggest you use mod()

There is no need to treat the first cycle differently: just initialize the queue to be empty.

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114483

I suggest you to use linspace()

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55230#comment_114493

I don't think linspace() will have much use in this question.