# Change variables in differential expressions

I have a fairly complicated differential expression in terms of a variable r and two unknown functions of r, B[r] and n[r]. I want to do a Taylor expansion of this around r=infinity. I want to do this by defining a new variable x=1/r and changing from r to x within my expression, then expanding around x=0.

Say the expression looks (more or less) like

n[r] (3 r B'[r]^2 - 4 B[r] (2 B'[r] + r B''[r]))


How do I turn this from something in terms of {r, n[r], B[r]} to something in terms of {x, n[x], B[x]}?

I'm not sure how to get Mathematica to work through the chain rule and change the dependent variable in the derivatives, and I also frequently get errors along the lines of "1/r is not a valid variable."

EDIT
I've managed to find at least a solution, although I'd imagine Mathematica has far more elegant ways of doing this. Hopefully if there's a cleaner way to do this someone will post it anyway. It would also be nice to have a more general method for changing variables as my way assumes that only up to second derivatives of B[r] and n[r] appear (since that happens to be true for this problem). Anyway, the solution I found was to do a replacement of the type

n[r] (3 r B'[r]^2 - 4 B[r] (2 B'[r] + r B''[r])) /.
{B'[r] -> B'[x]/D[1/x, x],
B''[r] -> D[(B'[x]/D[1/x, x]), x]/D[1/x, x],
n'[r] -> n'[x]/D[1/x, x],
n''[r] -> D[(n'[x]/D[1/x, x]), x]/D[1/x, x],
B[r] -> B[x],
n[r] -> n[x],
r  -> 1/x}


i.e., literally just replacing all of the derivatives w.r.t. r with derivatives w.r.t. x one by one, then replacing B[r] and n[r] with B[x] and n[x], then replacing r itself with 1/x. Not pretty but it does work.

FURTHER EDIT
If you want to do something like this, use Maple. Their PDETools has just the right function which I can't seem to find in Mathematica.

• Someone created a Mathematica analogue for the Maple function dchange to perform a change of variables in differential equations, that you mention in your edit. Here is the answer mathematica.stackexchange.com/a/80267/41148 Mar 16, 2021 at 11:58

I found the perfect way to do this by looking how to replace functions inside of a derivative. If we start with a function f[x] and want to replace x by g[x], then for the chain rule to be applied automatically, we simply write a replacement rule as follows:

f'[x] /. f -> (f[g[#]] &)


The output Mathematica gives me is

f'[g[x]] g'[x]


Which is the expected (and seeked!) chain rule.

• Perhaps you can improve the answer by actually applying the solution to the example in the OP, e.g. n[r](3 r..)? Oct 18, 2012 at 11:47

Alex already has found what I think will solve the problem of the OP, but hasn't adopted it to the original problem. Here is what that would look like:

expr = n[r] (3 r B'[r]^2 - 4 B[r] (2 B'[r] + r B''[r]))
expr /. {B -> (B[1/#] &), n -> (n[1/#] &)} /. r -> 1/x


Something like this ?

expr[r_] = n[r] (3 r D[b[r], r]^2 - 4 b[r] (2 D[b[r], r] + r D[b[r], {r, 2}]))

exp1 = Series[expr[1/x], {x, 0, 2}] // Normal

exp2 = Series[expr[r], {r, \[Infinity], 2}] // Normal

(exp2 /. r -> 1/x) == exp1

(* True *)

• @Verde I think the chain rule is applied correctly with D[b[1/x],{x,2}] (which is different from your expression). Aug 12, 2012 at 7:43
• I do not think the chain rule is applied correctly. If I use expr[r_]:=D[b[r],r] and then evaluate expr[1/x] I get b'[x] and not -b'[x] x^2... Aug 12, 2012 at 18:11
• @Fabian I think using = or := to define the function will be essentially different. Also expr[r_]:=D[b[r],r] will not allowed 1/x as a legal argument (at least in my MMA 8). By using =, the derivative is obtained before assigning to expr. The $-x^2$ in $-b'(x)x^2$ is nothing but the factor from coordinate transformation, wich makes $-b'(x)x^2=\mathrm db(1/x)/\mathrm d x=[\mathrm db(r)/\mathrm d r]/[\mathrm dr/\mathrm d x]$ (where $r=1/x$). Consider the identity relation $\mathrm df(r)=f'(r)\mathrm dr=f'(r(x))r'(x)\mathrm dx$ ==> $f'(r)=f'(r(x))$. Aug 13, 2012 at 18:07

You may find it useful to use pattern matching in your substituiton:

{D[B[r], {r, 1}], D[B[r], {r, 2}]}/.
D[f_[r], {r, i_}] ->
D[D[f[x], {x, i - 1}]/(D[1/x, x])^(i - 1) , x]/D[1/x, x]


(I don't know if that is correct for higher derivatives, but it capures the case you have shown)