简体   繁体   中英

Using symbols in solutions of a system of differential equations with sympy (python)

I'm trying to solve 2 coupled ODE using dsolve from sympy:

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)

The point is that when the gamma symbol (which is an undefined constant) is added to my equations as above, I have the following error message:

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

The solutions are fine when I remove gamma . Is it possible to use symbols in solutions of ODEs using sympy?

What version of sympy are you using?

This works fine with 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₂⋅ℯ                              ⎥
                                                                            ⎦

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM