Substitute known function's derivative

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?

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

• Sorry, I am a newbie in Mathematica. What is ^:=? I cannot find it in the documentation. – Arastas Jun 19 at 14:51
• @Arastas it invokes the function UpSetDelayed. I suggest looking at the documentation for this and the tutorial "Associating Definitions with Different Symbols" – mikado Jun 19 at 15:04
• @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. – Bob Hanlon Jun 19 at 15:44
• 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? – Arastas Jun 19 at 21:21
• 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. – mikado Jun 20 at 7:22