According to Cost of Capital approach to optimal financing mix we can calculate Cost-of-Capital-minimizing $\frac{D}{E}$ ratio as follows:
$\frac{D}{E}_{opt} = argmin_{\frac{D}{E}}WACC$,
where
$WACC = \frac{E}{D+E}r_{e} + \frac{D}{D+E}r_{d}(1-T)$
Cost of Equity ($r_{e}$) is determined by CAPM model (with given levels of riskfree rate $r_{f}$ and market premium $r_{m}$):
$r_{e} = r_{f} + \beta_{L}r_{m}$
Levered value of $\beta$ is determined by Hamada's equation:
$\beta_{L} = \beta_{UL}(1+ \frac{D}{E}(1-T))$
Cost of Debt ($r_{d}$) is determined using bond rating approach, where each interval of $\frac{D}{D+E}$ (in this case 0%-10%, 10%-20% and so on) is assigned with a certain interest rate on debt.
This gives us:
$WACC = \frac{E}{D+E}(r_{f} + \beta_{UL}(1+ \frac{D}{E}(1-T))r_{m}) + \frac{D}{D+E}r_{d}(1-T)$
Assume $\frac{D}{E} = X$ After all simplifications we have:
$WACC = \frac{1}{X+1}(r_{f} + \beta_{UL}r_{m}) + \frac{X}{X+1}(1-T)(r_{d} + \beta_{UL}r_{m})$
Now all we have to do to minimize is $\frac{\text{d}WACC}{\text{d}X} = 0$
Since $r_{d}$ is a function of $X$ and we would have to take $\frac{\text{d}r_{d}}{\text{d}X}$, we will instead seach the minimum on each interval (0%-10%, 10%-20% and so on), where $r_{d}$ will be constant.
Taking the derivative:
$\frac{\text{d}WACC}{\text{d}X} = \frac{(1-T)(r_{d} + \beta_{UL}r_{m}) - (r_{f} + \beta_{UL}r_{m})}{(X+1)^{2}}$
Assuming X is nonnegative, the only solution to
$\frac{(1-T)(r_{d} + \beta_{UL}r_{m}) - (r_{f} + \beta_{UL}r_{m})}{(X+1)^{2}} = 0$
is
$(1-T)(r_{d} + \beta_{UL}r_{m}) - (r_{f} + \beta_{UL}r_{m}) = 0$
All the variables here are constant and independent of $X$, which gives us no answer about the optimal $\frac{D}{E}$ ratio. Am I missing something here?