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


Groups > comp.soft-sys.math.mathematica > #5737 > unrolled thread

Problem with "Which"

Started bymbmb <sb@9y.com>
First post2011-10-20 11:49 +0000
Last post2011-10-21 11:28 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Problem with "Which" mbmb <sb@9y.com> - 2011-10-20 11:49 +0000
    Re: Problem with "Which" Rui <rui.rojo@gmail.com> - 2011-10-21 10:24 +0000
      Re: Problem with "Which" Tutor <alfred.muehlleitner@gmx.at> - 2011-10-22 10:17 +0000
    Re: Problem with "Which" "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-10-21 10:25 +0000
    Re: Problem with "Which" "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-10-21 11:28 +0000

#5737 — Problem with "Which"

Frommbmb <sb@9y.com>
Date2011-10-20 11:49 +0000
SubjectProblem with "Which"
Message-ID<j7p1t7$5ks$1@smc.vnet.net>
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?

[toc] | [next] | [standalone]


#5743

FromRui <rui.rojo@gmail.com>
Date2011-10-21 10:24 +0000
Message-ID<j7rh96$haq$1@smc.vnet.net>
In reply to#5737
Which tries it's arguments in turn until one yields true, AS LONG AS they yield false. If a condition yields neither True nor False, it returns the Which expression only partially evaluated, expecting that some day that expression might start yielding True or False. In your case, for example, if you ever assign x a value that makes -6+6 x be negative, then that Which will be resolved. Not sure what you expected... 

[toc] | [prev] | [next] | [standalone]


#5791

FromTutor <alfred.muehlleitner@gmx.at>
Date2011-10-22 10:17 +0000
Message-ID<j7u570$t90$1@smc.vnet.net>
In reply to#5743
On 21 Okt., 12:24, Rui <rui.r...@gmail.com> wrote:
> Which tries it's arguments in turn until one yields true, AS LONG AS they yield false. If a condition yields neither True nor False, it returns the Which expression only partially evaluated, expecting that some day that expression might start yielding True or False. In your case, for example, if you ever assign x a value that makes -6+6 x be negative, then that Which will be resolved. Not sure what you expected...

As you can see: {Which[f2 > 0, "T", f2 < 0, "H", f2 == 0, "S" ], x,
f} /. ew] this expression is evaluated and it yields true for each x
in ew.

I think, he is expecting someting like {{H,0,0},{T,2,-4}}

[toc] | [prev] | [next] | [standalone]


#5744

From"Dr. Wolfgang Hintze" <weh@snafu.de>
Date2011-10-21 10:25 +0000
Message-ID<j7rha5$hbe$1@smc.vnet.net>
In reply to#5737
There are some corrections necessary and improvements possible to your code.
Instead of explaining them one by one I present you a code that is 
close to yours and does what you want.
You can study it and find out where it differs from yours.

See below: first comes the code, then your example, finally the code is 
applied to random polynomials of given degree.

Have fun (Viel Spaß bei der Kurvendiskussion!)

---- Wolfgang

(* the code *)
KD[f0_] := Module[{x, ew, f, f1, f2},
f[x_] = f0[x]; f1[x_] = D[f[x], x];
f2[x_] = D[f[x], {x, 2}];
ew = Union[Re[x /. Solve[f1[x] == 0, x]]]; (* Re[] ignores complex 
roots; Union[] avoids repititions *)
Print["Extremwerte: ",
({Which[f2[#1] > 0, "T", f2[#1] < 0, "H",
f2[#1] == 0, "S"], #1, f[#1]} & ) /@ ew]]

(* example , notice that the function must b entered as a pure function 
here *)
fin = #1^3 - 3*#1^2 & ;
fin[x]
Plot[fin[x], {x, -2, 4}];
KD[fin]

-3*x^2 + x^3

"Extremwerte: "{{"H", 0, 0}, {"T", 2, -4}}

(* random polynomial of degree n *)
n = 4;
a = Array[r & , n];
fin = Sum[a[[i]]*#1^i, {i, 1, n}] & ;
fin[x]
Plot[fin[x], {x, -4, 4}];
KD[fin]

-x + x^2 + 3*x^3 + 3*x^4

"Extremwerte: "{
{"T", -1/4 - (2187 - 108*Sqrt[410])^(1/3)/72 -
(81 + 4*Sqrt[410])^(1/3)/24,
1/4 + (2187 - 108*Sqrt[410])^(1/3)/72 +
(81 + 4*Sqrt[410])^(1/3)/24 +
(-1/4 - (2187 - 108*Sqrt[410])^(1/3)/72 -
(81 + 4*Sqrt[410])^(1/3)/24)^2 +
3*(-1/4 - (2187 - 108*Sqrt[410])^(1/3)/72 -
(81 + 4*Sqrt[410])^(1/3)/24)^3 +
3*(-1/4 - (2187 - 108*Sqrt[410])^(1/3)/72 -
(81 + 4*Sqrt[410])^(1/3)/24)^4},
{"T", -1/4 + (2187 - 108*Sqrt[410])^(1/3)/36 +
(81 + 4*Sqrt[410])^(1/3)/12,
1/4 - (2187 - 108*Sqrt[410])^(1/3)/36 -
(81 + 4*Sqrt[410])^(1/3)/12 +
(-1/4 + (2187 - 108*Sqrt[410])^(1/3)/36 +
(81 + 4*Sqrt[410])^(1/3)/12)^2 +
3*(-1/4 + (2187 - 108*Sqrt[410])^(1/3)/36 +
(81 + 4*Sqrt[410])^(1/3)/12)^3 +
3*(-1/4 + (2187 - 108*Sqrt[410])^(1/3)/36 +
(81 + 4*Sqrt[410])^(1/3)/12)^4}}

(* end *)






"mbmb" <sb@9y.com> schrieb im Newsbeitrag 
news:j7p1t7$5ks$1@smc.vnet.net...
> 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?
> 

[toc] | [prev] | [next] | [standalone]


#5765

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-10-21 11:28 +0000
Message-ID<j7rl19$i7o$1@smc.vnet.net>
In reply to#5737
On Thu, 20 Oct 2011 12:49:59 +0100, mbmb <sb@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?
>

Try:

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];
]


KD[x^3 - 3 x^2, -1, 4]

Prints:

Extremwerte: {{H,0,0},{T,2,-4}}
Extremwerte: {{H,0,0},{T,2,-4}}

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web