I am only beginning to learn to use Mathematica, and got stuck while trying to implement what is apparently a simple substitution rule.

So I have a second order ODE in f, and I want to put in the ansatzf = r*phi, a multiplicative ansatz. I thought ReplaceAll followed by Simplify would do the job, but apparently this doesn't expand and simplify the derivatives. I want the replaced ODE to be in terms of r and phi only, no f, so that I can then manipulate them further. How to accomplish this?

Also, a side question, does someone know of a way to get to display the ODE in the proper form, all y'', y' and y terms grouped together? (for aesthetic purposes only)

The code:

J[k_,f_] := f''[k] +f'[k] +f[k]
J[k,f] //ReplaceAll[f[k] -> r[k] phi[k]] //Simplify

The output is :

r[k] phi[k] + f'[k] + f''[k]

I want the derivatives to be expanded/evaluated too.

  • $\begingroup$ Copy your Mathematica code here: what's the ODE? what's the ansatz you tried? What was the output? What did you intend to achieve? $\endgroup$
    – QuantumDot
    Commented Jul 23, 2016 at 17:57
  • 1
    $\begingroup$ Search for dchange on this site. $\endgroup$
    – march
    Commented Jul 23, 2016 at 18:27
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Commented Jul 23, 2016 at 19:07
  • 1
    $\begingroup$ Try f''[k] + f'[k] + f[k] /. f -> Function[k, r[k] phi[k]]. $\endgroup$ Commented Jul 23, 2016 at 21:03
  • 1
    $\begingroup$ With your syntax (delayed assignment of J), you could also do with J[k, r[k] phi[k]] $\endgroup$
    – Peltio
    Commented Jul 23, 2016 at 22:17

1 Answer 1


Two ways:

J[k_, f_] := f''[k] + f'[k] + f[k]
J[k, r[#] phi[#] &] // Simplify


J[k, f] /. f -> (r[#] phi[#] &) // Simplify


(*  {2 phi'[k] r'[k] + r[k] (phi'[k] + phi''[k]) + phi[k] (r[k] + r'[k] + r''[k])}  *)

One can go further, too:

J[k, f]/f[k] /. f -> (r[#] phi[#] &) // Expand;
% /. {r'[k] -> lr'[k] r[k], phi'[k] -> lphi'[k] phi[k], 
    r''[k] -> (lr''[k] + lr'[k]^2) r[k], 
    phi''[k] -> (lphi''[k] + lphi'[k]^2) phi[k]} // Factor;
% /. lr -> (Integrate[u[#], #] - lphi[#] &) // Expand
(*  1 + u[k] + u[k]^2 + u'[k]  *)

where $\int u(k) \;dk = \log r(k) + \log \phi(k)$.

  • $\begingroup$ Just noticed J.M. and Peltio had similar. suggestions in comments. $\endgroup$
    – Michael E2
    Commented Jul 24, 2016 at 0:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.