%% Homework # 3 Program 1

A = [274.7229, 90.2514, 67.6181, 50.1222, 53.3987, 35.3508];
G = [7.9932,   3.4998,  1.6883,  0.6644,  0.3756,   0.09  ];
R = [-1.5331,  1.0942,  3.9722,  7.7021, 10.2488,  15.9784];
      
for i = 1:6   

    P_loss   = exp(-(i)*log(10));

    for k = 0:3 

        N_max    =  k;
        P_target =  P_loss^( 1/(N_max +1) );
        Results  = [P_loss,N_max,P_target];

        for Mode=1:6
            a         = A(Mode);
            g         = G(Mode);
            r         = exp(0.1*R(Mode));
            step      = 1;
            Flag      = 0;
            n         = 0;
            
            while(Flag < 1)
                n       = n + 1;
                ave_SNR = r + n*step; 
                y       = 1-exp(-r/ave_SNR) + a*exp(-(g+1/ave_SNR)*r)/(1+ave_SNR*g);

                if(y <= P_target)
                    Flag   = 1;
                    value  = 10*log10(ave_SNR);
                end
            end

            Results   = [Results, value];

        end
        format short g 
        Results
    end
end