Re: Problem with "Which"
- To: mathgroup at smc.vnet.net
- Subject: [mg122210] Re: Problem with "Which"
- From: "Dr. Wolfgang Hintze" <weh at snafu.de>
- Date: Fri, 21 Oct 2011 06:22:16 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j7p1t7$5ks$1@smc.vnet.net>
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 at 9y.com> schrieb im Newsbeitrag
news:j7p1t7$5ks$1 at 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?
>