# 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[1][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[1][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, 2020 at 5:19
– Kuba
Jul 9, 2020 at 5:23
• energy /. {Subscript[R, s] -> (Subscript[r, s] a[#] &)} Jul 9, 2020 at 5:56

Quick solution:

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


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

• @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.