I am newbie in the matlab, may some experts tell me what are the meaning of codes or logic in this file?

8 visualizzazioni (ultimi 30 giorni)
Hello, I am a new user to MatLab, but I actually don't actually know what i should type in the matlab program to let me run my desired program.
Actually, I am doing my final year project with the mitigation and the simulation of the bus stop. In order to simulate the real scenario, I use a distribution and a program flow to simulate the bus congestion.
However, as I am new to Matlab, my program files are given by my supervisor and I don't know what is the logic flow or meaning of the codes in the files.
May some experts tell me the flow of the program codes and help me? Thank you very much.
The following are the codes.
Files: Sim_FIFO_MDc.m
% code
global size;
global Avg_Wait;
global Avg_Wait_Q; % Average wait time in queue per bus
global Serv_Rate;
global Arr_Rate;
global U0; %parameters same as Sim.m
global j0;
global IntArr;
global Arr;
global Serv;
global Wait;
global Wait_Q;
global Wait_S;
global Pos;
global c;
global Cs;
% Generate inter-arrival time vector
IntArr(1 : size) = -1 / Arr_Rate * log(U0(j0 : j0 + size - 1));
% Inter-arrival time follows an exponential distribution (i.e., bus arrivals are Poisson)
j0 = j0 + size;
% Generate service time vector
Serv(1 : size) = (1 / Serv_Rate) * ones(size, 1); % Service time is deterministic
% Serv(1 : size) = gamrnd(1 / Cs ^ 2, Cs ^ 2 * Serv_Rate, size, 1);
% Service time follows gamma distribution, where mean is 1, and coefficient of variation is Cs
Arr(1 : size) = cumsum(IntArr); % Calculate arrival times
Wait_Q(1) = 0;
Wait_S(1) = 0;
Pos(1) = 1;
for i = 2 : size % iterate for all the buses
if Pos(i - 1) == c
Wait_Q(i) = max(0, Wait_Q(i - 1) + Serv(i - 1) + Wait_S(i - 1) - IntArr(i));
else
Wait_Q(i) = max(0, Wait_Q(i - 1) - IntArr(i));
end
if Wait_Q(i - 1) + Serv(i - 1) + Wait_S(i - 1) < IntArr(i) + Wait_Q(i) + 0.0001
Pos(i) = 1;
else
Pos(i) = Pos(i - 1) + 1;
end
Wait_S(i) = max(0, Wait_Q(i - 1) + Serv(i - 1) + Wait_S(i - 1) - Wait_Q(i) - Serv(i) - IntArr(i));
end
Wait = Wait_Q + Wait_S;
Avg_Wait = mean(Wait);
Avg_Wait_Q = mean(Wait_Q);
end
% code
Files: Sim.m
% code
T_start = clock; % see if the program ends or not, ctrl+c can stop the program
global size; % number of buses in each round go into the bus stop
size = 200000; % set it into a high size because to reduce the fluctuation error
global Avg_Wait; % Average wait time per bus
global Serv_Rate; % Service rate (inverse of average service time per bus)
Serv_Rate = 1; %set service rate to one , don't care the unit at all.
global Arr_Rate; % Arrival rate of buses (i.e., bus flow) (eg, 3 buses per time unit(serv_rate))
global U0; %used for indexes
global j0;
%all the following global variables is having the size of "size".
global IntArr; % Vector for inter-arrival times (headway)
global Arr; % Vector for arrival times
global Serv; % Vector for service times
global Wait_Q; % Vector for bus wait times in queue
global Wait_S; % Vector for bus wait times in berths
global Wait; % Vector for bus wait times (sum of Q+S)
global Pos; % Vector for the berth numbers where buses dwell
global c; % Berth number (0,1,2) max=2 berths
global Cs; % Coefficient of variation in service time
M = 20000000; %random number generator
U0 = rand(M, 1); % Random seed (a vector of random numbers of size M, each is uniformly distributed between 0 and 1)
j0 = 1; %random take one number from 0 to 1
IntArr = zeros(size - 1, 1);
Arr = zeros(size, 1);
Serv = zeros(size, 1);
Wait = zeros(size, 1);
Wait_Q = zeros(size, 1);
Wait_S = zeros(size, 1);
Pos = zeros(size, 1);
Cs = 0.5; % coefficient of variation in service time
c = 2; % number of berths
%plotting the figure start from 2 because first one always zero.
for i0 = 2:20 % Need 20 data points on the curve
% Arr_Rate = 0.15, 0.25, ..., 1.95, subject to adjustment
Arr_Rate = 0.1 * (i0-1) + 0.05;
Sim_FIFO_MDc; %deterministic function service time
beta(i0) = Arr_Rate / c; % beta = bus flow per berth
W(i0) = Avg_Wait; % Average wait time per bus
if j0 > M - 200 * size %reset the U0 and j0 if data is not enough
U0 = rand(M, 1);
j0 = 1;
end
end
% First data point
beta(1) = 0;
W(1) = 0;
plot(beta, W);
T_end = clock;
T_dur = T_end - T_start;
fprintf('\nThe simulation takes %4.2f seconds.\n' ...
, T_dur(4) * 3600 + T_dur(5) * 60 + T_dur(6));
% code
  4 Commenti
Echo
Echo il 21 Dic 2014
Re: David,
I used MS C++ before,
And for this program, I knew the function of the program but I am not understanding the flow of ideas and the codes. So, I want to ask about this.

Accedi per commentare.

Risposte (2)

Image Analyst
Image Analyst il 20 Dic 2014
Since your "program files are given by my supervisor " he would be the one to ask, rather than with us, don't you think?
  4 Commenti
Echo
Echo il 21 Dic 2014
Yes, the comments are typed by him because I don't quite understand the flow and I asked him for this. So, he typed the comments for me.
And basically, I knew some of the MATLAB functions but i haven't type a program before.
Image Analyst
Image Analyst il 21 Dic 2014
That was very nice of him to help you by doing that, though he should have put the comments in as the code was being written originally - that's what the best programmers do. I do that. Sounds like he's very willing to help you until you fully understand his code, and I'd take advantage of his generosity.

Accedi per commentare.


David Young
David Young il 21 Dic 2014
For someone coming from the C++ background, I suspect one unfamiliar aspect of the code is the colon operator (:), which generates a vector containing a sequence of numerical values. For example,
3:6
produces the vector
[3 4 5 6]
Give the command "doc colon" to get more information and examples.
Then you need to become familiar with the idea that operators like + and * can be applied to the whole of a vector or matrix, so that what would be a loop in C++ becomes a single line of code. For example
[3 4 5] + 10
produces the vector
[13 14 15]
Again, you'll need to follow this up in the documentation.
On the overall structure, it looks like you call the script Sim.m to run the program, and it in turn calls Sim_FIFO_MDc.m. Calling scripts in this way is a bit like function call, except without input and output parameters and with a shared workspace for variables. Otherwise, the control structures ("for" and "if") are much as in other languages.
One thing that may be misleading is the presence of all the "global" declarations. These are unnecessary, since scripts all share a single workspace, so all their variables are shared.
The comments in the code look very helpful. You should be able to make progress by going through the code a line at a time, looking at the documentation for any construct you don't understand. You can even execute it a line at a time using the debugger, and then you can see exactly what it does and what the variable values are at each stage.
  2 Commenti
Echo
Echo il 21 Dic 2014
Wow, it is very nice that you tell me these. I am getting familiar with this now. But can we have more conversation to ask you about MATLAB via other communication channel like skype or any other else?
David Young
David Young il 21 Dic 2014
Sorry. I'm happy to answer a few questions here, but I'm afraid I don't have time to do individual help.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by