%Ponderea de eroare P(m,N)
p1=0.240053*10^(-2);%modelul tandem Gilbert-Elliot
q1=0.100485;
p2=0.278662*10^(-2);
q2=0.119516;
pg1=0.220591*10^(-3);
pg2=0.563331*10^(-3);
pb1=0.419360;
pb2=0.417201;
h=(p1+q1)*(p2+q2);
z1=q1*q2/h;
z2=q1*p2/h;
z3=p1*q2/h;
z4=p1*p2/h;
P1=pg1+pg2-2*pg1*pg2;
P2=pg1+pb2-2*pg1*pb2;
P3=pb1+pg2-2*pb1*pg2;
P4=pb1+pb2-2*pb1*pb2;
%pge1=(q1*pg1+p1*pb1)/(p1+q1);
%pge2=(q2*pg2+p2*pb2)/(p2+q2);
%ptge=(1-pge1)*pge2+(1-pge2)*pge1;
m=12;
N=15;
a=1:m;
p11=(1-p1)*(1-p2);p12=(1-p1)*p2;p13=p1*(1-p2);p14=p1*p2;
p21=(1-p1)*q2;p22=(1-p1)*p2;p23=p1*(1-p2);p24=p1*p2;
p31=q1*(1-p2);p32=q1*p2;p33=p1*(1-p2);p34=(1-q1)*p2;
p41=q1*q2;p42=q1*p2;p43=p1*(1-p2);p44=(1-q1)*(1-q2);

c1=zeros(m+1,N+1);
c2=zeros(m+1,N+1);
c3=zeros(m+1,N+1);
c4=zeros(m+1,N+1);
c1(1,1)=0;    c2(1,1)=0;    c3(1,1)=0;    c4(1,1)=0;
c1(1,2)=1-P1; c2(1,2)=1-P2; c3(1,2)=1-P3; c4(1,2)=1-P4;
c1(2,2)=P1;   c2(2,2)=P2;   c3(2,2)=P3;   c4(2,2)=P4;

for k=3:N+1
  c1(1,k)=c1(1,k-1)*p11*(1-P1)+c2(1,k-1)*p12*(1-P1)+c3(1,k-1)*p13*(1-P1)+c4(1,k-1)*p14*(1-P1);
  c2(1,k)=c1(1,k-1)*p21*(1-P2)+c2(1,k-1)*p22*(1-P2)+c3(1,k-1)*p23*(1-P2)+c4(1,k-1)*p24*(1-P2);
  c3(1,k)=c1(1,k-1)*p31*(1-P3)+c2(1,k-1)*p32*(1-P3)+c3(1,k-1)*p33*(1-P3)+c4(1,k-1)*p34*(1-P3);
  c4(1,k)=c1(1,k-1)*p41*(1-P4)+c2(1,k-1)*p42*(1-P4)+c3(1,k-1)*p43*(1-P4)+c4(1,k-1)*p44*(1-P4);
end

for i=2:m+1,
   for j=2:N+1,
     c1(i,j)=c1(i,j-1)*p11*(1-P1)+c2(i,j-1)*p12*(1-P1)+c3(i,j-1)*p13*(1-P1)+c4(i,j-1)*p14*(1-P1)+...
        c1(i-1,j-1)*p11*P1+c2(i-1,j-1)*p12*P1+c3(i-1,j-1)*p13*P1+c4(i-1,j-1)*p14*P1;
     c2(i,j)=c1(i,j-1)*p21*(1-P2)+c2(i,j-1)*p22*(1-P2)+c3(i,j-1)*p23*(1-P2)+c4(i,j-1)*p24*(1-P2)+...
        c1(i-1,j-1)*p21*P2+c2(i-1,j-1)*p22*P2+c3(i-1,j-1)*p23*P2+c4(i-1,j-1)*p24*P2;
     c3(i,j)=c1(i,j-1)*p31*(1-P3)+c2(i,j-1)*p32*(1-P3)+c3(i,j-1)*p33*(1-P3)+c4(i,j-1)*p34*(1-P3)+...
        c1(i-1,j-1)*p31*P3+c2(i-1,j-1)*p32*P3+c3(i-1,j-1)*p33*P3+c4(i-1,j-1)*p34*P3;
     c4(i,j)=c1(i,j-1)*p41*(1-P4)+c2(i,j-1)*p42*(1-P4)+c3(i,j-1)*p43*(1-P4)+c4(i,j-1)*p44*(1-P4)+...
        c1(i-1,j-1)*p41*P4+c2(i-1,j-1)*p42*P4+c3(i-1,j-1)*p43*P4+c4(i-1,j-1)*p44*P4;
   end
end

PtgemN=z1.*c1(2:13,N+1)'+z2.*c2(2:13,N+1)'+z3.*c3(2:13,N+1)'+z4.*c4(2:13,N+1)';

loglog(a,PtgemN)
  
