4
$\begingroup$

Suppose I know that the time derivative of $x(t)$ is $\dot{x}(t)=-x(t)$. I consider the function $V(x)=x^2$ and I want to compute the time derivative of $V$ along the trajectories of $x$, namely $\dot{V}(t) = 2x(t)\dot{x}(t) = -2x^2(t)$. I can do it like

v[x_] := x^2;
D[v[x[t]], t] /. D[x[t], t] -> -x[t]

But then I have to explicitly mention the substitution D[x[t], t] -> -x[t] everywhere. Is it possible to define it once, e.g., as a function definition? What is the good practice here?

$\endgroup$
5
$\begingroup$

Initially you have

v[x_] := x^2;
D[v[x[t]], t]
(* 2 x[t] Derivative[1][x][t] *)

to which you can apply the substitution.

If you want this substitution to happen automatically, you can define an "UpValue". This means that the definition is associated with x, rather than Derivative. Note the use of a pure function on the RHS.

Derivative[1][x] ^:= (-x[#] &)

You then get

D[v[x[t]], t]
(* -2 x[t]^2 *)

as desired

$\endgroup$
5
  • $\begingroup$ Sorry, I am a newbie in Mathematica. What is ^:=? I cannot find it in the documentation. $\endgroup$ – Arastas Jun 19 at 14:51
  • $\begingroup$ @Arastas it invokes the function UpSetDelayed. I suggest looking at the documentation for this and the tutorial "Associating Definitions with Different Symbols" $\endgroup$ – mikado Jun 19 at 15:04
  • 1
    $\begingroup$ @Arastas - when you don't recognize something like ^:=, highlight it in Mathematica and press F1 for help. Or, hover over it and click on the info icon in the tooltip. $\endgroup$ – Bob Hanlon Jun 19 at 15:44
  • $\begingroup$ I have found Defining Derivative section in documentation: reference.wolfram.com/language/tutorial/Calculus.html#1054 Is it different from your solution, or it is the same but written in a different way? $\endgroup$ – Arastas Jun 19 at 21:21
  • $\begingroup$ In Mathematica there's often more than one way to do things. I'm not sure whether the different syntax creates the same definition. You can examine the definitions created to see. $\endgroup$ – mikado Jun 20 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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