Re: Problem with "Which"

*To*: mathgroup at smc.vnet.net*Subject*: [mg122214] Re: Problem with "Which"*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Fri, 21 Oct 2011 06:23:00 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <20111020222623.8AB2DE672D@smtp.hushmail.com>*Reply-to*: drmajorbob at yahoo.com

Help has an example that helps explain it: "Conditions are evaluated until one is found that is neither True nor False: Which[1 < 0, a, x == 0, b, 0 < 1, c] Which[x == 0, b, 0 < 1, c]" As you can see, the first condition -- and the value after it -- are stripped away. That happens in your example too, and that seems to indicate that f2 > 0 resolved to False. The question is WHEN does it become False? Before /.ew is applied, or after? Clear[KD] KD[f0_] := Module[{f = f0, ew, f2}, ew = Solve[D[f, x] == 0, x, Reals]; f2 = D[f, {x, 2}]; Print[f2, "\n", f2 > 0, "\n", Which[f2 > 0, "T", f2 < 0, "H", f2 == 0, "S"], "\n", {f2, f2 > 0, Which[f2 > 0, "T", f2 < 0, "H", f2 == 0, "S"], x, f} /. ew];] KD[x^3 - 3 x^2] -6+6 x -6+6 x>0 Which[-6+6 x>0,T,f2$1576<0,H,f2$1576==0,S] {{-6,False,Which[-6+6 x<0,H,f2$1576==0,S],0,0},{6,True,T,2,-4}} As you see, f2 > 0 is neither True nor False before /.ew. After substitution it is False for x -> 0 and True for x -> 2. But x is substituted ONLY ONCE, and that's why f2 < 0 and f2 == 0 are unevaluated. The solution is to use ReplaceRepeated: Clear[KD] KD[f0_] := Module[{f = f0, ew, f2}, ew = Solve[D[f, x] == 0, x, Reals]; f2 = D[f, {x, 2}]; Print[ {Which[f2 > 0, "T", f2 < 0, "H", f2 == 0, "S"], x, f} //. ew];] KD[x^3 - 3 x^2] {{H,0,0},{T,2,-4}} If has only one condition and has attribute HoldRest, not HoldAll like Which, so one replacement is enough. And that's the difference. Bobby On Thu, 20 Oct 2011 17:26:23 -0500, <sb at 9y.com> wrote: > Thank you for your help. > > I have corrected "f2==0", but that didn't change anything. > > I want to evaluate the which-statement with my ew. f2 is then {- > 6,6} and this is correctly evaluated in the case, that the first > condition of which is true: {T,2,-4} > > It gets only unevaluated in the second expression of which, when f2 > is -6. > > You meant: When x -> 0, all three conditions fail again, since f2 = > 0 still resolves to 0, not True. > > When x->0, f2 gets -6 so the 2nd condition should be true. > > I don't understand, why the which expression is evaluated IF the > first case is true but not evaluated when the 2nd case is true. > > When I change the order of the conditions "Which[f2 < 0, "H", f2 > > 0, "T", f2 = 0, "S" ]..." than it is again ONLY correctly evaluated > for the first condition... > > Hope you can see what is troubling me > > > > On Thu, 20 Oct 2011 22:32:47 +0200 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 > -- DrMajorBob at yahoo.com

**Re: Problem with "Which"**

**Re: Initial value problem when using NDSolve for differential-algebraic system**

**Re: Problem with "Which"**

**Re: Problem with "Which"**