pythonsympysymbolic-mathdifferential-equations

Differential equation using dynamicsymbols in Sympy


In Sympy, I tried to solve a differential equation like this:

from sympy import *
from sympy.physics.vector import dynamicsymbols
x = dynamicsymbols('x')
diffeq = Eq(x(t).diff(t), x(t))
dsolve(diffeq, x(t))

But that returns

TypeError                           Traceback (most recent call last)
<ipython-input-10-8a45d7148b24> in <module>()
      1 x = dynamicsymbols('x')
----> 2 diffeq = Eq(x(t).diff(t), x(t))
      3 dsolve(diffeq, x(t))

TypeError: 'x' object is not callable

As far as I understand, dynamicsymbols creates a function of t, so how do I use it in a differential equation?


Solution

  • Sympy docs are a bit confusing... When you print the variable x

    print(x)
    

    you get the string

    x(t)
    

    However that doesn't mean are supposed to call x(t) in your equations, but instead just use the variable x:

    from sympy import *
    from sympy.physics.vector import dynamicsymbols
    x = dynamicsymbols('x')
    diffeq = Eq(diff(x, Symbol('t')), x)
    dsolve(diffeq, x)   # Eq(x(t), C1*exp(t))