This is a matter of evaluation order. First of all, notice under your definition, any w
that doesn't satisfy w < 0
will match the second definition, for example
Clear[e]
Y[0.5, e]
(* e *)
Then why does Plot
choose the first definition of Y
? Because, as mentioned in Details and Options section of document of Plot
:
Plot
has attribute HoldAll
and evaluates f
only after assigning specific numerical values to x
.
Then why does NIntegrate
behave differently? It owns attribute HoldAll
, too! That's because owning HoldAll
doesn't necessarily mean the argument will never be evaluated. Actually NIntegrate
evaluates the arguments once they're Block
ed. This is also mentioned in the document (in Details and Options section of document of NIntegrate
):
NIntegrate
first localizes the values of all variables, then evaluates f
with the variables being symbolic, and then repeatedly evaluates the result numerically.
This behavior has been discussed in the following posts, too:
NIntegrate evaluates its 1st argument while it has the attribute HoldAll?
Numerical Laplace transform error
Finally, as already mentioned by Ulrich Neumann, one can make Plot
behave like NIntegrate
by adding the undocumented option Evaluated
:
Plot[Y[0.5, e], {e, -2, -1}, Evaluated -> True]
Or simply Evaluate
:
Plot[Y[0.5, e] // Evaluate, {e, -2, -1}]
Is it possible to make NIntegrate
behave like Plot
? Sadly Evaluated
isn't an option of NIntegrate
, but we can make use of NumericQ
:
help[e_?NumericQ] := Y[0.5, e]
NIntegrate[help[e], {e, -2, -1}]
BTW, when defining piecewise functions, it's not a good idea to use Condition
(/;
). It doesn't work well with all the functions for simplification (Simplify
, etc.), it's not suitable for numeric evaluation, either. (Because it's based on pattern matching. ) Piecewise
is a much better choice. (Don't forget you can create it with EscpwEsc. )