Search code examples
pythonsympysymbolic-mathode

Using sympy dsolve for simple harmonic motion returns the incorrect solution


So i tried to use sympy's dsolve function to solve the very simple case of simple harmonic motion i.e.:

I did this like so:

w = sympy.symbols('ω', real = True, positive=True, nonzero=True)
t = sympy.symbols('t', real = True)
x = sympy.symbols('x')

eq = sympy.diff(sympy.diff(x(t), t), t) + w**2*x

sympy.pprint(sympy.dsolve(eq, x(t)))

When I do I get the following solution:

Instead of or as I expected.

Why is this?


Solution

  • You have not defined the d.e. properly. It should be

    eq = sympy.diff(sympy.diff(x(t), t), t) + w**2*x(t)
    

    Note that you can use the more readable .diff method

    eq = x(t).diff(t,2) + w**2*x(t)