# How can I write a function which takes another function of a variable and the variable itself as arguments and gives a list of critical points?

This is an exercise from Power Programming with Mathematica by Wagner. I need a function which takes a function and a variable as arguments, like this: critpts[f_,x_]:=

and then on the rhs I've got something like /;Derivative[f[x]]=0 as a condition at the end, and what I want is something that will generate a list of rules like x->0 which describe the values of critical points. An example would be that if the function worked it'd mean something like this: critpts[x^2,x] would generate something like this: x->0. I don't even know how I can generate a list of rules at all.

-
Take a look at Solve –  sebhofer Aug 12 '14 at 7:48
Why a capital F in .../;Derivative[F[x]]=0? –  hieron Aug 12 '14 at 9:29
It was a mistake, and should be a lower-case f. –  Tobias Nash Aug 12 '14 at 9:45
Maybe you mean someExpression/;f'[x]==0, also Equal and not Set –  hieron Aug 12 '14 at 9:46
You should consider giving your function the attribute HoldAll. –  m_goldberg Aug 12 '14 at 10:01

First, how you would you find the critical points of a function, say $f(x)=x^3-x$, from scratch? I guess you might write

D[x^3 - x, x]
(* Out: 3x^2 - 1 *)


Then, you want to know when that's equal to zero. So you might type

Solve[% == 0, x]
(* Out: {{x -> -(1/Sqrt[3])}, {x -> 1/Sqrt[3]}} *)


where the % sign refers to the previous output.

Now at this point, we notice a couple of things. First, the Solve command (which you'll certainly use in your function) returns a list of rules - exactly as you want and said that you weren't certain how to produce. Second, rather than using % to refer to the previous output, you might simply chain the commands into a single command. So you might have something like

Solve[D[x^3 - x, x] == 0, x]


Finally, for your program, you probably don't want to be restricted to $x^3-x$, so let's assign a symbol to that - perhaps f.

f = x^3 - x;
Solve[D[f, x] == 0, x]


Well, there's your code - you've just got to use it to define a function.

critpts[f_, x_] := Solve[D[f, x] == 0, x];


Let's try it

critpts[x^3 - x, x]
critpts[Sin[x], x]
critpts[t*Exp[-t^2], t]

(* Out:
{{x -> -(1/Sqrt[3])}, {x -> 1/Sqrt[3]}}

{{x -> ConditionalExpression[-Pi/2 + 2*Pi*C[1], Element[C[1], Integers]]},
{x -> ConditionalExpression[Pi/2 + 2*Pi*C[1], Element[C[1], Integers]]}}

{{t -> -(1/Sqrt[2])}, {t -> 1/Sqrt[2]}}
*)


Naturally, it will inherit many of the restrictions of the Solve command. Thus, the following produces an error and returns an unevaluated Solve.

In[253]:= critpts[t*Sin[t], t]
(* Out: Solve[t Cos[t] + Sin[t] == 0, t] *)

-
Maybe you could add why the OP shouldn't use critpts[f_,x_]/;f'[x]==0 as he was trying to do? –  sebhofer Aug 12 '14 at 15:50