Another approach that may be useful is the following:
ClearAll["Global`*"]
f = (x + μ)^2 + y^2;
g = (x + μ - 1)^2 + y^2;
rule = {
D[d1, z_, NonConstants -> {d1, d2}] :> D[f, z]/2/d1,
D[d2, z_, NonConstants -> {d1, d2}] :> D[g, z]/2/d2
};
V = m1/d1 + m2/d2 + 1/2*(x^2 + y^2);
D[V, x, NonConstants -> {d1, d2}] /. rule
D[V, y, NonConstants -> {d1, d2}] /. rule
The motivation for this approach is to make the replacement rule a little more general and to take advantage of the NonConstants
option of the derivative function. To understand this approach, we consider the expression
ClearAll["Global`*"]
V = m1/d1 + m2/d2 + 1/2*(x^2 + y^2);
D[V, x, NonConstants -> {d1, d2}]
which gives us $\partial_x V$ in terms of D[d1,x,NonConstants -> {d1,d2}]
. We write our rule to express $\partial_x d_1$ in terms of d
as follows
D[d1, z_, NonConstants -> {d1, d2}] :> D[f, z]/2/d1
There are several things to note in the above rule. First, it uses variable f
, which is explained in the next paragraph. Second, is uses the delayed rule function :>
. Third, it assumes $d_1=\sqrt{f}$ so that $\partial_x d_1=(\partial_x f)/(2d_1)$. Fourth, we are using z
to name the argument of the derivative function, so the rule will work for $\partial_x$ and $\partial_y$, but z
must be an undefined symbol.
So, to summarize, this approach defines f
and g
, the arguments of the Sqrt
function, creates rules for taking the derivatives of d1
and d2
, and applies the rules to the derivative of V.
The advantage of this approach is that we can change f
and g
without affecting the rest of the code, which could sometimes be desirable. We can, in fact, change or define f
and g
anywhere before the rule is applied. That is, we can define the rule first, take the derivative of $V$, then define f
andg
before applying the rule.
Another delayed rule that may be handy is r2 = {d1 :> Sqrt[f], d2 :> Sqrt[g]}
.
D[V,x]
andD[V,y]
is to look atFullForm[D[V[x]]
andFullForm[D[V[y]]
, where in the first you will see that the denominator comes fromPower[Plus[Power[y,2],Power[Plus[-1,x,\[Mu]],2]],Rational[-3,2]]]
, i.e. $(y^2 + (-1 + x + \mu)^2)^{-3/2}$. In particular, nothing has an exponent of $1/2$. So either your rules have to be modified to match what will be present, or they need to have parameters, as in the accepted solution. $\endgroup$