4
$\begingroup$

Let's see the following example

Clear["Global`*"];
d1 = Sqrt[(x + μ)^2 + y^2];
d2 = Sqrt[(x + μ - 1)^2 + y^2];
V = m1/d1 + m2/d2 + 1/2*(x^2 + y^2);

rule = {((x + μ)^2 + y^2)^(1/2) -> d1, ((x + μ - 1)^2 + y^2)^(1/2) -> d2};
Vx = D[V, x] /. rule
Vy = D[V, y] /. rule

However even if I apply the rule the output is not what I want. I want the two denominators to have the form $d1^3$ and $d2^3$. Is this possible?

Many thanks in advance!

$\endgroup$
1
  • $\begingroup$ A way to diagnose why your rules aren't acting on D[V,x] and D[V,y] is to look at FullForm[D[V[x]] and FullForm[D[V[y]], where in the first you will see that the denominator comes from Power[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$ Commented Nov 29, 2016 at 17:11

2 Answers 2

9
$\begingroup$

You can use patterns in your replacement rules:

Clear["Global`*"];
D1 = Sqrt[(x + μ)^2 + y^2];
D2 = Sqrt[(x + μ - 1)^2 + y^2];
V = m1/D1 + m2/D2 + 1/2*(x^2 + y^2);

rules = {((x + μ)^2 + y^2)^n_ ->  d1^(2 n), 
        ((x + μ - 1)^2 + y^2)^n_ -> d2^(2 n)}
Vx = D[V, x] /. rules
Vy = D[V, y] /. rules

Explanation: x^n_ -> t^(2 n) replaces any power of x to t raised twice the exponent, therefore Sqrt[x]^n gets replaced by t^n.

$\endgroup$
1
  • $\begingroup$ Nice trick! Thanks! $\endgroup$
    – Vaggelis_Z
    Commented Nov 29, 2016 at 10:09
0
$\begingroup$

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]}.

$\endgroup$

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.