function  RollGeskeWhaley= RollGeskeWhaley( s, x, v, r , T, D, TD )
%UNTITLED5 Summary of this function goes here
%   Detailed explanation goes here
BIGNUM= 100000000;
EPSILON= 0.00001;
mCall = 1;
SX = s - D * exp(-r * TD);
% Not optimal to exercise.....
if (D <= x * (1 - exp(-r * (T - TD))))
    RollGeskeWhaley = GBlackScholes(mCall, SX, x, T, r, r, v);
else
   ci = GBlackScholes(mCall, SX, x, T - TD, r, r, v);
   HighS = s;
   
   while ((ci - HighS - D + x) > 0 && (HighS < BIGNUM))
       HighS = 2 * HighS;
       ci = GBlackScholes(mCall, HighS, x, T - TD, r, r, v);
   end

   if (HighS < BIGNUM)
       RollGeskeWhaley = GBlackScholes(mCall, SX, x, T, r, r, v);
   else
       LowS = 0;
       i = HighS * 0.5;
       ci = GBlackScholes(mCall, i, x, T - TD, r, r, v);
% Find the critical Stock Price with Bisection.
   while (Abs(ci - i - D + x) > EPSILON && (HighS - LowS) > EPSILON)
       if ((ci - i - D + x) < 0)
           HighS = i;
       else
           LowS = i;
       end
       i = (HighS + LowS) / 2;
       ci = GBlackScholes(mCall, i, x, T - TD, r, r, v);
   end
        a1 = (log(SX / x) + (r + v * v / 2) * T) / (v * sqrt(T));
        a2 = a1 - v * sqrt(T);
        b1 = (log(SX / i) + (r + v * v / 2) * TD) / (v * sqrt(TD));
        b2 = b1 - v * sqrt(TD);
        c = SX * (xCND(b1) + M(a1, -b1, sqrt(TD / T))) - x * exp(-r * T) * M(a2, -b2, -sqrt(TD / T)) - (x - D) * exp(-r * TD) * xCND(b2);
        RollGeskeWhaley = c;
   end
      
end
end


