p=0.278662*10^(-2);%modelul tandem Bernoulli-Gilbert
q=0.119516;
pg=0.105521*10^(-1);
pb=0.418857;
m=12;
a1=1:12;
a=1:13;

%crearea matricelor initiale
g=zeros(m+1,N);
b=zeros(m+1,N);
g(1,:)=(1-pg)*[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
g(2,:)=pg*[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
b(1,:)=(1-pb)*[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
b(2,:)=pb*[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

%calculul elementelor prin recurenta
for k=2:N
   g(1,k)=g(1,k-1)*(1-p)*(1-pg)+b(1,k-1)*p*(1-pg);
   b(1,k)=b(1,k-1)*(1-q)*(1-pb)+g(1,k-1)*q*(1-pb);
end

for i=2:m,
   for j=2:N,
     g(i,j)=g(i,j-1)*(1-p)*(1-pg)+g(i-1,j-1)*(1-p)*pg+b(i,j-1)*p*(1-pg)+b(i-1,j-1)*p*pg;
     b(i,j)=b(i,j-1)*(1-q)*(1-pb)+b(i-1,j-1)*(1-q)*pb+g(i,j-1)*q*(1-pb)+g(i-1,j-1)*q*pb;
   end
end

%calculul ponderii de eroare
PgemN=(q/(p+q))*g(2:13,N).'+(p/(p+q))*b(2:13,N).';

loglog(a1,PgemN)