Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5754

Re: Problem with "Which"

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Problem with "Which"
Date 2011-10-21 10:30 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j7rhkn$het$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 10/20/11 at 7:44 AM, sb@9y.com (mbmb) 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?

Mathematica is evaluating f2 to 6 x - 6 in all cases. But,
Mathematica cannot do the comparison until a value is
substituted for x. It is the substitution that is creating the problem.

In the case of the If, there is only one substitution per value
of x to be done. This happens, the comparison takes place you
are expecting and you get the output you expect.

In the case of Which, the substitution occurs in the first
expression with x only. So, when 0 is substituted for x, the
first test evaluates as false and no other test gets evaluated.
Then when 2 is substituted for x, the first test evaluates as
true and not other test gets evaluated.

Since nothing evaluates as true when 0 is substituted for x, you
get the Which statement returned unevaluated.

Also, note your code as

Which[f2 > 0, "T", f2 < 0, "H", f2 = 0, "S" ]

instead of

Which[f2 > 0, "T", f2 < 0, "H", f2 == 0, "S" ]

That is your last "test" sets f2 to 0 which I suspect is not
what you intended

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Problem with "Which" Bill Rowe <readnews@sbcglobal.net> - 2011-10-21 10:30 +0000

csiph-web