Issue with sympy dsolve and zero

91 Views Asked by At

In the following code, I want to have the friction constant 'c' be zero, however, if I put it equal to zero, the dsolve() function hangs up. It works fine if I put it equal to 1e-20 (effectively zero)

I guess using 1e-20 is working but I would like to understand why this is happening and if it is a fault in sympy, which it seems it is?

enter image description here

# differential equations for Forced oscillation oscillating mass on spring

import sympy as sy
import numpy as np

x = sy.Function('x')
t,m,k,c,g   = sy.symbols('t m k c g')

c=1e-20  # not sure why if I chang the friction constant zero the dsolve function hangs
m=1.00   # mass
k=9      # spring constant

g=9.8    # gravity

f=x(t)

g=80*sy.cos(5*t)  # Forcing function
x_0=0  # initial position of x
d_x_0=0 # inital 1st diff of x

edo = sy.Eq(m*x(t).diff(t,t) + c*x(t).diff(t) + k*x(t),g) #  differential equation mass m on a spring k  with friction c

sy.pprint(edo)

sol = sy.dsolve(edo, f, ics={f.diff(t).subs(t,0):x_0, f.subs(t, 0): d_x_0})

sy.pprint(sol.n(2))

sy.plot((sol.rhs), (t,0,10))
1

There are 1 best solutions below

4
On

The hanging appears to be caused by the float number m=1.0. Better use symbols in the equation, then solve and finally substitute numerical values:

c, m, k = sy.symbols("c, m, k")
# substitution dictionary
sd = {c: 0, m: 1, k: 9}

g=80*sy.cos(5*t)  # Forcing function
x_0=0  # initial position of x
d_x_0=0 # inital 1st diff of x

edo = sy.Eq(m*x(t).diff(t,t) + c*x(t).diff(t) + k*x(t), g) #  differential equation mass m on a spring k  with friction c

sy.pprint(edo)

sol = sy.dsolve(edo, f, ics={f.diff(t).subs(t,0):x_0, f.subs(t, 0): d_x_0})

sy.plot(sol.rhs.subs(sd), (t,0,10))