I want to find an elegant way of solving the following differential equation:
from sympy import *
init_printing()
M, phi, t, r = symbols('M phi t r')
eq = Eq(-M * phi(t).diff(t), Rational(3, 2) * m * r**2 * phi(t).diff(t) * phi(t).diff(t,t))
I assume that phi(t).diff(t) is not zero. Hence the left and right side are shortened.
This is how I get to the solution:
# I assume d/dt(phi(t)) != 0
theta = symbols('theta')
eq = eq.subs({phi(t).diff(t, 2): theta}) # remove the second derivative
eq = eq.subs({phi(t).diff(t): 1}) # the first derivative is shortened
eq = eq.subs({theta: phi(t).diff(t, 2)}) # get the second derivative back
dsolve(eq, phi(t))
How do I solve this more elegantly?
Ideally dsolve()
would be able to solve the equation directly, but it doesn't know how (it needs to learn that it can factor an equation and solve the factors independently). I opened an issue for it.
My only other suggestion is to divide phi' out directly:
eq = Eq(eq.lhs/phi(t).diff(t), eq.rhs/phi(t).diff(t))
You can also use
eq.xreplace({phi(t).diff(t): 1})
to replace the first derivative with 1 without modifying the second derivative (unlike subs
, xreplace
has no mathematical knowledge of what it is replacing; it just replaces expressions exactly).
And don't forget that phi(t) = C1
is also a solution (for when phi' does equal 0).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.