0

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

0

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))
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you David, should not this work either way, as in it is a fault in the code somewhere?
The problem with using symbols in the equation is that you will get different results for a differential equation depending on the values, so you sometimes need the values in there before going to dsolve. I feel like this is a fault in sympy dsolve as in it does not consider all situations correctly, i.e. if you set c and k to zero, it just locks up the kernel completely where it should return a simple quadratic. Do you know where I can take sympy problem to please?
You can open an issue here, explaining the problem in detail: github.com/sympy/sympy/issues
I just read that if I use rational numbers, rather than floats it might work better. I will try that first and then log the issue either way as it would clearly be better if it worked with reals. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.