[英]Using symbols in solutions of a system of differential equations with sympy (python)
我正在嘗試使用來自 sympy 的 dsolve 解決 2 個耦合 ODE:
import sympy as sp
t, gamma = sp.symbols('t, gamma', real=True)
rho1, rho2 = sp.symbols('rho_1, rho_2', cls=Function)
drho1 = sp.Derivative(rho1(t), t)
drho2 = sp.Derivative(rho2(t), t)
eq1 = sp.Eq(drho1, - 2*gamma*(rho1(t) - rho2(t)))
eq2 = sp.Eq(drho2, - 2*(rho2(t) + rho1(t)))
eq = (eq1,eq2)
sp.dsolve(eq)
關鍵是當伽馬符號(這是一個未定義的常數)被添加到我的方程時,我有以下錯誤消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-e7f194f9576a> in <module>()
13 eq = (eq1,eq2)
14
---> 15 sp.dsolve(eq, ics=ics)
3 frames
/usr/local/lib/python3.6/dist-packages/sympy/core/relational.py in __nonzero__(self)
193
194 def __nonzero__(self):
--> 195 raise TypeError("cannot determine truth value of Relational")
196
197 __bool__ = __nonzero__
TypeError: cannot determine truth value of Relational
當我刪除gamma時,解決方案很好。 是否可以使用 sympy 在 ODE 的解中使用符號?
您使用的是什么版本的 sympy?
這適用於 1.6.2:
In [4]: eqs = [eq1, eq2]
In [5]: eqs
Out[5]:
⎡d d ⎤
⎢──(ρ₁(t)) = -2⋅γ⋅(ρ₁(t) - ρ₂(t)), ──(ρ₂(t)) = -2⋅ρ₁(t) - 2⋅ρ₂(t)⎥
⎣dt dt ⎦
In [6]: sol = dsolve(eqs)
In [7]: sol
Out[7]:
⎡ ⎛ ______________ ⎞ ⎛ ______________ ⎞ ⎛ ______________ ⎞ ⎛ ______________ ⎞
⎢ ⎜ ╱ 2 ⎟ ⎜ ╱ 2 ⎟ ⎜ ╱ 2 ⎟ ⎜ ╱ 2 ⎟
⎢ ⎜γ ╲╱ γ - 6⋅γ + 1 1⎟ t⋅⎝-γ - ╲╱ γ - 6⋅γ + 1 - 1⎠ ⎜γ ╲╱ γ - 6⋅γ + 1 1⎟ t⋅⎝-γ + ╲╱ γ - 6⋅γ + 1 - 1⎠
⎢ρ₁(t) = C₁⋅⎜─ + ───────────────── - ─⎟⋅ℯ + C₂⋅⎜─ - ───────────────── - ─⎟⋅ℯ , ρ₂(
⎣ ⎝2 2 2⎠ ⎝2 2 2⎠
⎛ ______________ ⎞ ⎛ ______________ ⎞⎤
⎜ ╱ 2 ⎟ ⎜ ╱ 2 ⎟⎥
t⋅⎝-γ - ╲╱ γ - 6⋅γ + 1 - 1⎠ t⋅⎝-γ + ╲╱ γ - 6⋅γ + 1 - 1⎠⎥
t) = C₁⋅ℯ + C₂⋅ℯ ⎥
⎦
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.