%MAKEMC.m  test-make MC     6sept98
%revised transitions 15sept98
%uses Act,Sp,lam,theta,L,C,R.  K is the interval I.
% Also uses EZ.m for exp and Cmean, mkQ.m for `spares MChain.'
% Should be followed by AVA.m to compute availability

%P=trans probs, U=mean uptime, D=mean downtime

N=Act+Sp;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sp>0 is first block of code. Sp=0 is separate block at end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if Sp > 0

%total number of states is
	tot=N+1+Sp;

%initialize for right size and zeros
P=zeros(tot,tot);U=P;D=P;

%non-recovery, system-up states 1 thru Sp+1
KK=K;XX=Act*lam;EZ;
%M is the mean number of additional intervals of length K til fail
% 	like a Bernoulli trial
M=ez/(1-ez);

KK=1/XX;mkQ; 
%mkQ computes for the associated continuous- parameter MC called S

%only exiting transitions are for act-fail 
for i=1:Sp+1 
	P(i,Sp+2)=Q(i,Sp+1);
	U(i,Sp+2)=M*(K-C);D(i,Sp+2)=M*C+L+Cmean;
   for j=N+2:tot 
	k=j-N-1;
	P(i,j)=Q(i,k);
	U(i,j)=U(i,Sp+2);D(i,j)=D(i,Sp+2);
end;end;

%recovery states N+2 thru tot=N+1+Sp
%	transitions for not(act-fail):
KK=R+K+L;EZ;mkQ;
for i=N+2:tot for j=1:Sp+1
	k=i-N-1;
	P(i,j)=ez*Q(k,j);U(i,j)=K;D(i,j)=R;
end;end;

%	now act-fail transitions for Sys Recov states
KK=Cmean;mkQ;
for i=N+2:tot
	P(i,Sp+2)=(1-ez)*Q(i-N-1,Sp+1);D(i,Sp+2)=Cmean;
   for j=N+2:tot
	k=i-N-1;l=j-N-1;
	P(i,j)=(1-ez)*Q(k,l);D(i,j)=Cmean;
end;end;

%system-down (too few procs) states Sp+2 thru N
%corrected for state Sp+2 below
%merging Sys Dwn states into a single representative isn't done here
if Act  > 1
for i=(Sp+2):N
	XX=(N-i+1)*lam + (i-1)*theta;
	P(i,i+1)=(N-i+1)*lam/XX;
	P(i,i-1)=(i-1)*theta/XX;
	D(i,i+1)=1/XX;D(i,i-1)=D(1,i+1);
end

%now fix the first recovery from sys-fail state Sp+2:
P(Sp+2,tot)=P(Sp+2,Sp+1);P(Sp+2,Sp+1)=0;
D(Sp+2,tot)=1/XX;D(Sp+2,Sp+1)=0;

%`all-failed' sys-fail state N+1:
	XX=N*theta;
	P(N+1,N)=1;
	D(N+1,N)=1/XX;
end

if Act==1 P(Sp+2,tot)=1;D(Sp+2,tot)=1/(N*theta); end

%this ends SP > 0 code
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Here's Sp==0: sys-up state 1 and recovery state N+2 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (Act> 1 & Sp==0) 

Q=0;q=0;RM=0;

N=Act+Sp;
tot=Act + 2;
P=zeros(tot,tot);U=P;D=P;

% the only sys-up state 1
XX=Act*lam; KK=K; EZ; M=ez/(1-ez);
P(1,2)=1; U(1,2)=M*(K-C);
D(1,2)= M*C + L+ Cmean;

%the only recovery state N+2
KK=R+K+L;EZ;
	P(N+2,1)=ez;U(N+2,1)=K;D(N+2,1)=R;
	P(N+2,2)=1-ez;D(N+2,2)=Cmean;

% sys-fail state 2
XX=(Act-1)*lam + theta;
	P(2,N+2)=theta/XX;
	P(2,3)=(Act-1)*lam/XX;
	D(2,N+2)=1/XX;D(2,3)=D(2,N+2);

%system-down (too few procs) states 3 thru N
for i=3:N
	XX=(N-i+1)*lam + (i-1)*theta;
	P(i,i+1)=(N-i+1)*lam/XX;
	P(i,i-1)=(i-1)*theta/XX;
	D(i,i+1)=1/XX;D(i,i-1)=D(i,i+1);
end;

%`all-failed' sys-fail state N+1:
XX=N*theta;
P(N+1,N)=1;
D(N+1,N)=1/XX;
end;

if (Act==1 & Sp==0) 

N=1;
tot=3;
P=zeros(3,3);U=P;D=P;Q=0;

% the only sys-up state 1
KK=K;XX=Act*lam;EZ;
	P(1,1)=ez;U(1,1)=K-C;D(1,1)=C;
	P(1,2)=1-ez;D(1,2)=L+Cmean;

% recovery state 3
KK=R+K+L;EZ;
	P(3,1)=ez;U(3,1)=K;D(3,1)=R;
	P(3,2)=1-ez;D(3,2)=Cmean;

% sys-fail state 2
XX=theta;
	P(2,3)=1;
	D(2,3)=1/XX;

%end of the road
end;
