%
% Test of Discrete Event Systems - 21.12.2020
%
% Additional exercises
%
% This script computes the numerical results of Exercise 3
%

clear all
close all
clc

% parameters
q0 = 0.3;
q1 = 0.4;
q2 = 0.3;
p0 = 0.1;
p1 = 0.2;
p2 = 0.4;
p3 = 0.3;

% transition probabilities
p00 = q0+q1*(p1+p2+p3)+q2*(p2+p3);
p01 = q1*p0+q2*p1;
p02 = q2*p0;
p03 = 0;
p10 = q0*(p1+p2+p3)+q1*(p2+p3)+q2*p3;
p11 = q0*p0+q1*p1+q2*p2;
p12 = q1*p0+q2*p1;
p13 = q2*p0;
p20 = q0*(p2+p3)+(q1+q2)*p3;
p21 = q0*p1+(q1+q2)*p2;
p22 = q0*p0+(q1+q2)*p1;
p23 = (q1+q2)*p0;  
p30 = p3;
p31 = p2;
p32 = p1;
p33 = p0;

% transition probability matrix
P = [ p00 p01 p02 p03 ; ...
    p10 p11 p12 p13 ; ...
    p20 p21 p22 p23 ; ...
    p30 p31 p32 p33 ];

% limit probabilities
n = size(P,1);
pi = ([ eye(n)-P'; ones(1,n) ]\[ zeros(n,1) ; 1 ])';
% alternatively: pi = [ 1 0 0 0 ]*P^T with T very large, e.g. T = 1e6

% QUESTION #2
EX = [ 0 1 2 3 ]*pi'

% QUESTION #3
n = 3;
p3 = (1-p22)*p22^(n-1)

% QUESTION #4
Ptilde = [ p00 p01 p02 p03 ; ...
    p10 p11 p12 p13 ; ...
    0 0 1 0 ; ...
    p30 p31 p32 p33 ]; 
t = 8;
p4 = 1 - [ 1 0 0 0 ]*Ptilde^t*[ 0 0 1 0 ]'

% QUESTION #5
Pbar = [ p00 p01 p02 p03 ; ...
    p10 p11 p12 p13 ; ...
    p20 p21 p22 p23 ; ...
    1 0 0 0 ]; 
n = size(Pbar,1);
pibar = ([ eye(n)-Pbar'; ones(1,n) ]\[ zeros(n,1) ; 1 ])';
ET03 = 1/pibar(4)-1

% QUESTION #6
Phat = [ 0 p01 p02 p03 p00 ; ...
    0 p11 p12 p13 p10 ; ...
    0 p21 p22 p23 p20; ...
    0 0 0 1 0 ; ...
    0 0 0 0 1 ];
T = 1e6;
p6 = [ 1 0 0 0 0 ]*Phat^T*[ 0 0 0 1 0 ]'