I am trying to create a realistic model of traffic conditions, and I want to create a random velocity generator to model this. Problem is, with a range of 0 to 50 MPH, the random numbers can vary wildly between this range between the iterations of the while loop (I am using each iteration as a second). I would like to create a range inside a range that would limit the next random number from being too far away from the previous one. Buses, for instances, might be able to accelerate from 0 – 10 MPH in a few seconds, but not from 0 – 50 MPH.
i = 0 sigma = 0.125 % % of the instant. vel.
t = 0V_t_old = 5x_1 = []x_2 = []x_3 = []% Passenger weight
prompt = 'Number of iterations 'pass_count = input(prompt) % This will be coming from the pass counter from Qlik
wv = [] % weight vector
for n = 1:pass_count S_t = randi([0 40]) %instaneous velocity (random) MPH
V_t = (sigma*S_t)+(1-sigma)*V_t_old wv(n) = V_t V_t_old = V_t if V_t <= 40 && V_t >= 30 x_3(n) = V_t elseif V_t < 30 && V_t >= 20 x_2(n) = V_t elseif V_t < 15 && V_t > 0 x_1(n) = V_t end end plot(wv)format longG
Best Answer