Search code examples
pythonsympygraphing

Python(sympy) : How to graph smoothly in 2nd ODE solution with Sympy?


I'm studing about structural dynamic analysis. I solved a problem : 1 degree of freedom

The question is m*y'' + cy' + ky = 900 sin(5.3x) m=6938.78, c=5129.907, k=379259, y is the function of x

I solved it's response using by Python and Sympy library. I drew the response by pyplot. But it's shape is not smooth like below enter image description here

How can I draw the respone smoothly?

I tried to draw smoothly by substituting each x to y by numpy, but could not insert x into sin(5.3x).

from sympy import *
import matplotlib.pyplot as plt

x, y=symbols("x, y")
f=symbols('f',cls=Function)
y=f(x)

eq=Eq( 6938.78*diff(y,x,2)  +  5129.907*diff(y,x) + 379259*y-900*sin(5.3*x),0)

eq_done=dsolve(eq,y, ics={  f(0):0,  diff(y,x).subs(x,0):0   }  )

plot(eq_done.rhs,(x,0,10))

Solution

  • To get a smoother line you can turn off the adaptive algorithm and set the number of points per line:

    plot(eq_done.rhs,(x,0,10), adaptive=False, nb_of_points=1000)
    

    Also, the help() function is your friend, as it allows to quickly access the documentation of a particular function. Execute help(plot) to learn more about the plot command.