don_Gunner94
don_Gunner94

Reputation: 144

I am trying to simulate a mass, on a friction less floor, with a spring, fixed at one end. Not working

I am using the standard differential equation for SHM for the above simulation, a = -w^2*x. I'm using Python with the odeint being the solver. Despite editing it several times, I keep getting the output as a straight line instead of a sinusoidal curve. The code is:

from scipy.integrate import odeint
from pylab import *

k = 80 #Spring Constant
m = 8  #mass of block
omega = sqrt(k/m) #angular velocity

def deriv(x,t):
    return array([x[1],(-1)*(k/m)*x[0]])

t = linspace(0,3.62,100)

xinit = array([0,0])

x = odeint(deriv,xinit,t)

acc_mass = zeros(t.shape[0])

for q in range(0,t.shape[0]):
    acc_mass[q] = (-1)*(omega**2)*x[q][0]

f, springer = subplots(3, sharex = True)
springer[0].plot(t,x[:,0],'r')
springer[0].set_title('Position Variation')
springer[1].plot(t,x[:,1],'b')
springer[1].set_title('Velocity Variation')
springer[2].plot(t,acc_mass,'g')
springer[2].set_title('Acceleration Variation')

Upvotes: 1

Views: 271

Answers (2)

don_Gunner94
don_Gunner94

Reputation: 144

As pointed out by Warren Weckesser, the code is correct, but since the initial conditions are given as 0 for the displacement, the output is also 0. Hence on his advice, I changed the initial conditions and got the required output which was a sinusoidal curve.

Upvotes: 2

ErikR
ErikR

Reputation: 52039

Here is a complete example of SHM using odeint:

http://nbviewer.ipython.org/gist/dpsanders/d417c1ffbb76f13f678c#2D-equations

Upvotes: 1

Related Questions