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] *)
Solve
$\endgroup$.../;Derivative[F[x]]=0
? $\endgroup$someExpression/;f'[x]==0
, also Equal and not Set $\endgroup$HoldAll
. $\endgroup$