1function [Se, Sestar, R0, Ke, Kc, S_long, S_last] = constructSRK( C, services, service_h, S)
3dim = length(service_h.beta);
4m = length(services.tau_st);
6indexes_notbusy = build_index(m,1);
7dim_NB = size(indexes_notbusy,1);
12dim_notbusy = (dim_C-1)*dim_NB+1;
14Se = zeros(newdim+dim_notbusy,newdim+dim_notbusy);
17Se(1:newdim,1:newdim) = S;
19%% from busy to not-busy
20% when the job in the shorter queue complete service
21S_long = zeros(dim,dim_NB);
25 countvect = service_h.service_phases(row,:);
27 for i = m+1:2*m % the starting phase
30 tovect = countvect(1:m);
32 col = vectmatch(tovect,indexes_notbusy);
34 % the job in the shorter queue completes service
35 S_long(row,col) = S_long(row,col)+countvect(i)*services.St(i-m);
41Se(1:dim,newdim+1:newdim+dim_NB) = S_long;
43 Se((row-1)*dim+1:row*dim,newdim+(row-2)*dim_NB+1:newdim+(row-1)*dim_NB) = S_long;
46% when the 2 queue have the same length
47S_last = zeros(dim,dim_NB);
51 countvect = service_h.service_phases(row,:);
57 tovect = countvect((2-k)*m+1:(2-k+1)*m);
59 col = vectmatch(tovect,indexes_notbusy);
61 S_last(row,col) = S_last(row,col)+countvect(i)*services.St(i-(k-1)*m);
68Se((dim_C-1)*dim+1:dim_C*dim,newdim+(dim_C-2)*dim_NB+1:newdim+(dim_C-1)*dim_NB) = S_last;
70Sestar(1:newdim,newdim+1:end) = Se(1:newdim,newdim+1:end);
72%% from not-busy to not-busy
75 Se(newdim+(row-1)*dim_NB+1:newdim+row*dim_NB,newdim+(row-1)*dim_NB+1:newdim+row*dim_NB) = services.ST;
78A = -sum(services.ST,2)*services.tau_st;
81 Se(newdim+(row-1)*dim_NB+1:newdim+row*dim_NB,newdim+row*dim_NB+1:newdim+(row+1)*dim_NB) = A;
84Se(newdim+(dim_C-2)*dim_NB+1:newdim+(dim_C-1)*dim_NB,newdim+(dim_C-1)*dim_NB+1:end) = -sum(services.ST,2);
86for row = newdim+1 : newdim+dim_notbusy
88 Se(row,row) = -sum(Se(row,:));
92%% Construct R0 matrix: from the not-busy period to a busy period
93% focus on the shortest queue, the system has only one idle server, thus an
94% arrival leads the system back to a busy-period
95% first dim phases: busy; second dim phases: not-busy
96% the queue jumps to the same phase in a busy period after an arrival
97R0 = zeros(newdim+dim_notbusy,newdim+dim_notbusy);
98R_NB = zeros(dim_NB,dim);
101 countvect = indexes_notbusy(row,:);
103 tovect_short = zeros(1,m);
105 tovect = [countvect,tovect_short];
106 col = vectmatch(tovect,service_h.service_phases);
107 R_NB(row,col) = R_NB(row,col)+services.tau_st(i);
113 R0(newdim+(row-1)*dim_NB+1:newdim+row*dim_NB,(row-1)*dim+1:row*dim) = R_NB;
116R0(end,newdim-dim+1:newdim) = service_h.beta;
118Ke = cat(2,eye(newdim),zeros(newdim,dim_notbusy));
119Kc = cat(1,eye(newdim),zeros(dim_notbusy,newdim));
122Sestar = sparse(Sestar);