# Invalid comparison in Manipulate with Stepwise function

I want to draw a graph of the function that I specified using Piecewise. Though I can draw a graph, I keep getting Min::nord/Max::nord errors saying invalid comparison with -xx + yy I attempted. The followings are my codes.

 ublb1[α_, H_, L_] := 1/2 (2 H - H^2 - 2 L + H L - H α + 2 L α -
H L α) -
1/2 Sqrt[-4 H^3 + H^4 + 4 H^2 L - 2 H^3 L + H^2 L^2 +
4 H^2 α + 2 H^3 α - 6 H^2 L α +
2 H^3 L α - 2 H^2 L^2 α - 3 H^2 α^2 +
2 H^2 L α^2 + H^2 L^2 α^2]

test[α_, H_, L_] :=
Piecewise[{{ublb1[α, H, L],
0 < L < H < 1 &&
2 L < H && (H - L)/(1 - H + H^2 - H L) <= α <= (
H^2 - 2 H L + H^2 L + L^2 - H L^2)/(H^2 - H L + L^2 - H L^2) &&
ublb1[α, H, L] ∈ Reals &&
ublb1[α, H, L] <= (1 - α) (1 - H) (H - L)}, {Null,
True}}]

Manipulate[
Plot[{test[α, H, L]}, {α, 0, 1}, AxesOrigin -> {0, 0},
PlotRange -> {0, 1}, AspectRatio -> 1, Frame -> True], {H, 0,
1}, {L, 0, H/2}]


I don't understand why I keep getting the errors about the comparison between the complex number and the real number even though I already include the condition ublb1[α, H, L] ∈ Reals && ublb1[α, H, L] <= (1 - α) (1 - H) (H - L).

• You probably need another condition in there. ublb1[0.3, 0.5, 0.2] is a complex number. It's coming from the Sqrt. For instance this is negative: With[{\[Alpha] = 0.3, H = 0.5, L = 0.2}, -4 H^3 + H^4 + 4 H^2 L - 2 H^3 L + H^2 L^2 + 4 H^2 \[Alpha] + 2 H^3 \[Alpha] - 6 H^2 L \[Alpha] + 2 H^3 L \[Alpha] - 2 H^2 L^2 \[Alpha] - 3 H^2 \[Alpha]^2 + 2 H^2 L \[Alpha]^2 + H^2 L^2 \[Alpha]^2 ] Also you should write ublb1[\[Alpha]_, H_, L_] := ... with underscores in the arguments of ublb1 as you're using SetDelayed like this. – flinty Jul 31 '20 at 23:22
• @flinty Oops. The underscore thing was a mistake. I know some parameter values will make the value inside the squared root negative so that the complex number will appear. My point is that I already give a condition that compare ublb1[alpha,H,L] with (1-alpha)(1-H)(H-L) only when ublb1[alpha,H,L] is real number, but I keep getting the errors regarding the comparison between complex number and the real number, which I don't understand. – Chris Jul 31 '20 at 23:33
• You need H^2 (H - L - [Alpha] + L [Alpha]) (-4 + H - L + 3 [Alpha] + L [Alpha]) > 0 (or the thing in the sqrt to be 0. (1-alpha)(1-H)(H-L) is not enough. – flinty Aug 1 '20 at 0:08

I resolved the issue by re-defining ublb1 as follows.

ublb1[α_, H_, L_] :=
Piecewise[{{
1/2 (2 H - H^2 - 2 L + H L - H α + 2 L α -
H L α) -
1/2 Sqrt[-4 H^3 + H^4 + 4 H^2 L - 2 H^3 L + H^2 L^2 +
4 H^2 α + 2 H^3 α - 6 H^2 L α +
2 H^3 L α - 2 H^2 L^2 α - 3 H^2 α^2 +
2 H^2 L α^2 + H^2 L^2 α^2],
Im[ 1/2 (2 H - H^2 - 2 L + H L - H α + 2 L α -
H L α) -
1/2 Sqrt[-4 H^3 + H^4 + 4 H^2 L - 2 H^3 L + H^2 L^2 +
4 H^2 α + 2 H^3 α - 6 H^2 L α +
2 H^3 L α - 2 H^2 L^2 α - 3 H^2 α^2 +
2 H^2 L α^2 + H^2 L^2 α^2]] == 0}, {Null,
True}}];