# Substitute variable in differential equation

I have

m = function('m')(r)
V = function('V')(r)
phi = m(r)/r
V = 4*pi*r^3/3
divG = (diff(phi,r,2)+2/r*diff(phi,r)).full_simplify()


which describes a spherical symmetric source term of a field, nice. Now I want to get this as a function of volume, not of radius and tried:

a=divG.subs(r=V(r))


But it gets me nowhere - It should also substitute the differentials - How to solve this?

edit retag close merge delete

Sort by » oldest newest most voted If I am right, you want to transform the differential expression $$\frac{d^2\phi}{dr^2}+\frac{2}{r}\frac{d\phi}{dr},$$ where $$\phi(r)=\frac{m(r)}{r},$$ by changing the independent variable to $V$. The relation between $V$ and $r$ is $$V=\frac{4\pi}{3} r^3.$$ We can think of $m(r)$ as the result of composing two functions: one that yields $V$ in terms of $r$ and then one that yields $m$ in terms of $V$. In other words, an abusing a bit of the notation, $$m(r)=m(V(r)).$$

We can then transform the given differential expression as follows:

var("r,V")
vol = 4*pi*r^3/3
m(V) = function("m")(V)    # m as function of V
M = m(V.subs(V=vol))       # this is m(r)=m(V(r))
phi = M/r
divG = (diff(phi,r,2) + 2/r*diff(phi,r))
divG = divG.subs(vol==V, r^3==3*V/(4*pi)).full_simplify()
show(divG)


In a Jupyter notebook, one gets

$$12 \pi V \frac{\partial^{2}}{(\partial V)^{2}}m\left(V\right) + 8 \pi \frac{\partial}{\partial V}m\left(V\right)$$

that is, in a more proper mathematical notation,

$$4\pi\left( 3V\frac{d^2m}{dV^2}(V)+2\frac{dm}{dV}(V)\right)$$

I hope this has some meaning for you.

more

Thanks a lot for showing me. - I got the first term of the solution by hand, but missed the second. The result has a meaning, but what it is I have to figure out, it seems the bracket contains sort of a "density operator".

Just 2 more questions: How do I insert latex code here in this forum? How are the symbols D0 and D0,0 defined (result from show(divG), divG in the first version as function from r)?

1) This forum uses MathJax to render LaTeX code. You just have to write it as you would do in a TeX file. For example, if you write

This is inline math: $V(r)=4\pi r^3/3$. This is display math:
$$V(r)=\frac{4\pi}{3}r^3.$$


you get, as expected,

This is inline math: $V(r)=4\pi r^3/3$. This is display math: $$V(r)=\frac{4\pi}{3}r^3.$$

2) If you insert print(divG), you will see that those symbols come from D[0,0](m) and D(m). For a multivariate function $f$, D[i,j,k...](f) means the partial derivative of $f$ respect to the $i$-th variable, then the $j$-th variable, then the $k$-th variable and so on. For example, for $f(x,y,z)$, D[2,0,1,2](f) corresponds to $$\frac{\partial^4f}{\partial z\partial x\partial y\partial z},$$ that is, $$\frac{\partial^4f}{\partial x\partial y\partial z^2}$$ if $f\in C^4$. Take into account that, in Python, indices start counting in $0$.

For a univariate function, D(f), D[0,0](f), etc. are just their successive derivatives.