% new code: Oct 6, 2010. For JSAC Paper.
function [totalpowperbit]=CalcTotalPowerGivenP(P)

global dv dc p iter pedesired pecurrent;

global Rdata Rdec Rch W boltzmann temperature thermalNoise pathlossexp r lambda1;
global pathloss Enode degree pe pedesired iter;




%%%
R = 1 - dv/dc;

p = qfunc(sqrt(2*P/pathloss/thermalNoise));

pe(1) = p;


l = 1000;

for iter = 2:l
    
    pe(iter) = newerrprob(pe(iter-1));
    
%    pe(iter)
    
    if pe(iter)<pedesired

        
        break;
        
    end
    
end
%iter
pecurrent = pe(iter);

%if iter == l
    
%    disp('iter = l');
%end

%if iter<l
    
%    iter
%    P
%end
totalpowperbit = P + Enode * Rdec * iter * (1+1/Rch) / Rch;
