I believe you are varying D, E, Rd, and I (they are finite in your example), and that you seek to optimize WACC. In general if you vary 2 independent values, S and D/E, to obtain the dependent value, WACC, you can manually seek the max value by plotting
pcolor(S, D./E, WACC)
Or find the max value as follows,
max(max(WACC(S<0.36, :))) % max over both rows and columns
assuming WACC(S, D./E) is a 2D matrix and S and D./E are vectors. [CAPM = capital asset pricing model, WACC = weighted average cost of capital. New terms to me!]
