# How do I make a substitution to an expression inside a derivative? [duplicate]

Pardon if I didn't ask the question correctly. Still trying to figure out the language. I have the following expression: $$energy=\frac{1}{2}R_s^{'}[t]^2==\frac{4}{3}G\pi\rho R_s[t]^2$$

energy = (1/2)*Derivative[Subscript[R, s]][t]^2 == (4/3)*G*
Pi*\[Rho]*Subscript[R, s][t]^2


I have the following identity: $$R_s[t]==a[t]r_s$$ When I make this substitution: $$energy/.R_s[t]\rightarrow a[t]r_s$$

energy /. Subscript[R, s][t] -> a[t]*Subscript[r, s]


I get this result:

$$\frac{1}{2}R_s^{'}[t]^2 == \frac{4}{3}G\pi\rho R_s[t]^2$$

(1/2)Derivative[Subscript[R, s]][t]^2 == (4/3)GPi[Rho]a[t]^2 Subscript[r, s]^2

What do I need to do for the derivative $$R_s^{'}[t]^2$$ to understand the substitution. I was expecting the result to be:

$$\frac{1}{2}r_s a'[t]^2 == \frac{4}{3}G\pi\rho (r_s a[t])^2$$

EDIT: Accessing external code is a bad idea and should be avoided at all costs. It's just one more thing that can go wrong when sharing code. I'm looking for a solution that solves this problem with the native Mathematica instructions.

• Why isn't R_s in the right hand side of the final result substituted? – Kuba Jul 9 '20 at 5:19
• Please see the duplicate thread and topics linked there. – Kuba Jul 9 '20 at 5:23
• energy /. {Subscript[R, s] -> (Subscript[r, s] a[#] &)} – Bill Watts Jul 9 '20 at 5:56

## 1 Answer

Quick solution:

energy /. Subscript[R, s] -> Function[t, a[t]*Subscript[r, s]] Or using linked answer:

DChange[energy, Subscript[R, s][t] == a[t]*Subscript[r, s]]

• I can't speak for everyone with the same problem, but your first solution seems to be the simplest one as it's a single line and uses just Mathematica. I'm not a big fan of external code in my solutions. One more thing to go wrong. – Quarkly Jul 9 '20 at 10:22
• @Quarkly sure, fair enough. Linked topic links to old solutions that are basically the same as the first line here so the duplicate works like a central hub rather than the only way to go. DChange uses the same method btw. – Kuba Jul 10 '20 at 7:33