Re: Problem with "Which"
- To: mathgroup at smc.vnet.net
- Subject: [mg122213] Re: Problem with "Which"
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Fri, 21 Oct 2011 06:22:49 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201110201144.HAA05724@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
Wait, there's a subtlety I got wrong, as the following shows:
Quit
{Which[f2 > 0, "T", f2 < 0, "H", f2 = 0, "S"], x, f}
{Which[f2 > 0, "T", f2 < 0, "H", f2 = 0, "S"], x, f}
f2
f2
f2 has no value, despite the "condition" f2 = 0 being reached. If f2 were
set to 0 either before or after substituting x -> 0, substituting x -> 2
would give the very same result, and that's not what happened. Hence,
there's an evaluation order issue I was missing.
It would no longer matter, however, if you used f2 == 0 as you must have
intended.
Bobby
On Thu, 20 Oct 2011 15:32:47 -0500, DrMajorBob <btreat1 at austin.rr.com>
wrote:
> In your Which statement, when x has not yet been substituted, neither f2
> > 0 nor f2 < 0 is True, so "f2 = 0" is reached.
>
> (You meant f2 == 0.)
>
> The value of f2 = 0 is zero, not True, so all three conditions have
> failed and Which remains unevaluated.
>
> The value of ew (which probably should have been a localized Module
> variable but isn't) is
>
> ew
>
> {{x -> 0}, {x -> 2}}
>
> When x -> 0, all three conditions fail again, since f2 = 0 still
> resolves to 0, not True.
>
> When x -> 2, the condition f2 > 0 is True, so Which[...] becomes "T",
> and that's what you're getting.
>
> In the If statement, f2 is still 0, having been set that way in the
> Which statement, so Which resolves to "T".
>
> The code did what you told it to do.
>
> Bobby
>
> On Thu, 20 Oct 2011 06:44:54 -0500, mbmb <sb at 9y.com> wrote:
>
>> Who can check my module?
>>
>> KD[f0_, a0_, b0_] := Module[{f = f0, a = a0, b = b0},
>> f2 = D[f, {x, 2}];
>> ew = Solve[D[f, x] == 0, x, Reals];
>> Print["Extremwerte: ", {Which[f2 > 0, "T", f2 < 0, "H", f2 = 0, "S" ],
>> x, f} /. ew];
>> Print["Extremwerte: ", {If[f2 > 0, "T", "H" ], x, f} /. ew];
>> ]
>>
>> When I enter: KD[x^3 - 3 x^2, -1, 4] the output is
>>
>> Extremwerte: {{Which[-6+6 x<0,H,f2=0,S],0,0},{T,2,-4}}
>>
>> whereas the IF-line gives
>>
>> Extremwerte: {{H,0,0},{T,2,-4}}
>>
>> Why can't I use Which in this case. Why doesn't Mathematica evaluate f2
>> in all cases?
>>
>
>
--
DrMajorBob at yahoo.com
- References:
- Problem with "Which"
- From: mbmb <sb@9y.com>
- Problem with "Which"