1
$\begingroup$

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.

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

1 Answer 1

2
$\begingroup$

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] *)
$\endgroup$
1
  • $\begingroup$ Maybe you could add why the OP shouldn't use critpts[f_,x_]/;f'[x]==0 as he was trying to do? $\endgroup$
    – sebhofer
    Commented Aug 12, 2014 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.