使用Matlab中的Lindley方程模拟队列

时间:2016-03-09 16:11:05

标签: matlab queue

我必须使用Lindley的等式在Matlab中模拟一个简单的队列:

W_{n+1}^Q = max(0, W_n^Q + S_n - X_{n+1}

我想我已经这样做了,使用以下代码,但我试图运行它几次并且无法正确保存信息。我想在运行模拟结束时保存的变量只保存上次尝试的信息(此处为m = 3)..虽然我明确希望看到所有运行的信息(m = 1,2,3) )。

for m=1:3
l = 1.1; % try this value for lambda
N = 10000; % let 1000 people arrive

X = exprnd(l,[1,N]); % make 1000 exponential interarrivals
S = 2*rand(1,N); % uniform on [0,2]
w = zeros(1,N);

sum1 = zeros(1,m);
avg1 = zeros(1,m);
max1 = zeros(1,m);

for i=1:N
    if i==1 % first customer doesn't have to wait
        w(i) = 0;
    else % following customers follow lindley's equation
        w(i) = max(w(i-1) + S(i-1) - X(i), 0); % n-th customer's waiting time
        count(i) = w(i) > 15; % count number of times greater than 15
    end

end
        max1(m) = max(w);
        sum1(m) = sum(count); % sum number of times greater than 15
        avg1(m) = sum1(m)/N; % divide by 1000 to get probability delay is greater than 15
end

1 个答案:

答案 0 :(得分:1)

您正在 for 循环中初始化 sum1 avg1 max1 ,因此在每次迭代中,这些变量被设置为零(即通过初始化)。这就是您丢失先前迭代值的原因。要避免这种情况,请在 for 循环之前初始化 sum1 avg1 max1 。请参考以下代码以供参考。 HTH

-S