File: spring1.py

package info (click to toggle)
pycode-browser 1%3A1.02-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,824 kB
  • ctags: 1,172
  • sloc: python: 2,232; xml: 152; makefile: 39
file content (25 lines) | stat: -rwxr-xr-x 617 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#spring1.py
from pylab import *
t = 0.0         # Stating time
dt = 0.01       # value of time increment
x = 10.0         # initial position
v = 0.0         # initial velocity
k = 10.0       # spring constant 
m = 2.0         # mass of the oscillating object

tm = []         # Empty lists to store time, velocity and displacement
vel = []
dis = []

while t < 5:
  f = -k * x                    # Try (-k*x - 0.5 * v) to add damping
  v = v +  (f/m ) * dt          # dv = a.dt 
  x = x + v * dt                # dS = v.dt 
  t = t + dt
  tm.append(t)
  vel.append(v)
  dis.append(x)

plot(tm,vel)
plot(tm,dis)
show()