python - Sympy:求解微分方程

标签 python sympy

我想找到一种优雅的方法来求解以下微分方程:

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))

enter image description here

我假设 phi(t).diff(t) 不为零。因此,左侧和右侧被缩短。

这就是我找到解决方案的方法:

# 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

enter image description here

dsolve(eq, phi(t))

enter image description here

如何更优雅地解决这个问题?

最佳答案

理想情况下,dsolve() 能够直接求解方程,但它不知道如何求解(它需要知道它可以对方程进行因式分解并独立求解这些因式)。我打开了issue为了它。

我唯一的其他建议是直接将 phi' 相除:

eq = Eq(eq.lhs/phi(t).diff(t), eq.rhs/phi(t).diff(t))

您还可以使用

eq.xreplace({phi(t).diff(t): 1})

用 1 替换一阶导数而不修改二阶导数(与 subs 不同,xreplace 没有关于替换内容的数学知识;它只是精确替换表达式) .

并且不要忘记 phi(t) = C1 也是一个解决方案(当 phi' 等于 0 时)。

关于python - Sympy:求解微分方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340217/

相关文章:

python - 使用稀疏张量为 TensorFlow 中的 softmax 层提供占位符

python - 计算列表 1 中的项目在列表 2 中出现的次数

python - 找出对象的可能属性

python - 智能重写 sympy 中的表达式

python - 在 python 中使用 sympy 求解器的困难

python - 使用 python 执行带有输入的 C 程序

python:从外部代码导入 numpy 作为 np 在我自己的用户定义模块中丢失

python - 打印sympy输入函数

python - 仅将 SymPy 中的 doit() 函数应用于表达式的外部

python - Sympy,是否可以强制一个类留在表达式的左侧或右侧