0
$\begingroup$

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

 ublb1[\[Alpha]_, H_, L_] := 1/2 (2 H - H^2 - 2 L + H L - H \[Alpha] + 2 L \[Alpha] - 
    H L \[Alpha]) - 
 1/2 Sqrt[-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]

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

Manipulate[
 Plot[{test[\[Alpha], H, L]}, {\[Alpha], 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[[Alpha], H, L] [Element] Reals && ublb1[[Alpha], H, L] <= (1 - [Alpha]) (1 - H) (H - L).

Thanks for your support in advance.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – flinty Jul 31 at 23:22
  • $\begingroup$ @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. $\endgroup$ – Chris Jul 31 at 23:33
  • $\begingroup$ 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. $\endgroup$ – flinty Aug 1 at 0:08
0
$\begingroup$

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}}];
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.