Untitled

 avatar
unknown
plain_text
a year ago
1.7 kB
7
Indexable
Web VPython 3.2
scene = canvas(background = color.white)

g = 9.8
mu = 0.008
L = 2.5
k = 5
#Natural length
L_0 = 2
mass = 2

ground = box(pos = vec(0,-0.02,0), size = vec(3,0.02,0.4), color = color.green)

b_box = box(pos = vec(-1,0.1,0), size = vec(0.4, 0.2, 0.1), color = color.red)

s_box = box(pos = b_box.pos + vec(L, 0, 0), v = vec(0,0,0), m = mass, size = vec(0.2, 0.2, 0.1), color = color.blue)

spring = helix(pos = b_box.pos, axis = s_box.pos - b_box.pos, radius = 0.05, color = color.orange)

s_box.p = s_box.m * s_box.v

#Velocity graph
vgraph = graph(title = "Velocity", xtitle = "t", ytitle = "v(t)")

#Velocity curve
vx_s = gcurve(graph = vgraph, color = color.cyan)

#Friction graph
fgraph = graph(title = "Force", xtitle = "t", ytitle = "N")

#Friction curves
fscurve = gcurve(graph = fgraph, color = color.orange)
ffcurve = gcurve(graph = fgraph, color = color.green)

t = 0
dt = 0.01

s_box.old_v = s_box.v
s_box.T = 0

scene.waitfor("click")

while t < 20:
    rate(1000)
    
    spring.axis = s_box.pos - b_box.pos
    
    L = s_box.pos - b_box.pos 
    
    spring.s = mag(L) - L_0
    
    #Hooke's Law
    F_s = -k * spring.s * L.hat
    
    #Friction force
    F_f = -mu * mass * g * s_box.v.hat
    
    #Total force on system
    F = F_s + F_f
    
    s_box.p = s_box.p + F*dt
    s_box.v = s_box.p / s_box.m
    s_box.pos = s_box.pos + s_box.v * dt
    
    vx_s.plot(pos = (t, s_box.v.x))
    
    fscurve.plot(t, F_s.mag)
    ffcurve.plot(t, F_f.mag)
    
    t += dt
    
    if s_box.old_v.x > 0 and s_box.v.x < 0:
        print('period: ', s_box.T, '; analytical: ', 2*pi*sqrt(s_box.m/k))
        s_box.T = 0
        
    s_box.old_v = s_box.v
    s_box.T += dt






Editor is loading...
Leave a Comment