Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Aug 12 '12 at 15:26

This question came from our site for professional and enthusiast programmers.

4 Answers 4

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.

share|improve this answer
    
Perhaps you can improve the answer by actually applying the solution to the example in the OP, e.g. n[r](3 r..)? –  FredrikD Oct 18 '12 at 11:47
    
@Alex: if you want to adopt your answer to the original problem, I'll delete my answer... –  Albert Retey Oct 18 '12 at 13:20

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
share|improve this answer

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 *)
share|improve this answer
    
@Verde I think the chain rule is applied correctly with D[b[1/x],{x,2}] (which is different from your expression). –  b.gatessucks Aug 12 '12 at 7:43
3  
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... –  Fabian Aug 12 '12 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))$. –  Silvia Aug 13 '12 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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.