function [n] = returnnforg2(g)

global Pt;
global log10pe;
global g2 Rch;

g2 = g;

%Pt
%log10pe


p = qfunc(sqrt(Pt));

Cg = 1 - hb(g);

deltag = 1 - Cg/Rch;

hbinvdeltag = hbinv(deltag);

% epsilonprime = sqrt(  (4/g)*  ( log2(4) - log2(hbinvdeltag) )  );

[optimalpoint, fg] = fminbnd(@epsilonbound,0,1-g2);

%epsilon = 0.001:0.001:1-g-0.001;

%epsilong = epsilon+g;

%bound = (1./epsilon.^2).*(   (1-(epsilong)).*log((1-(epsilong))./(1-g))  + (epsilong).*log((epsilong)/g))  ;

%fg = min(bound)


epsilonprime = sqrt ( (1/fg)  *  ( log2(2)  - log2(hbinvdeltag)   )   );

c = log10pe*log2(10) - log2(hbinvdeltag) + 1;

%c = log10pe*log2(10) - log2(deltag/2) - log2(hbinv(deltag/2)/2) +1 ;
%            c = - log2(1/pe(t))  - log2(hbinv(deltag)) +1 ;


    div = g * log2(g/p)   +   (1-g) * log2((1-g)/(1-p));

    a = div;

    b = log2(  (g*(1-p))  /  (p*(1-g))  )   * epsilonprime;

    if (b^2 - 4*a*c >0)
        sqrtn = ( (- b + sqrt(b^2 - 4*a*c) )/ ( 2*a)    );

    else
        sqrtn = 1;
    end

n = -max(sqrtn^2,1);



