lab5

 avatar
unknown
python
4 months ago
1.5 kB
2
Indexable
Web VPython 3.2

from visual import *

scene = canvas(background = color.white)

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

g = 9.8
mu = 0.01
L = 2.5
k = 5
L_0 = 2
mass = 2


anchor = box( pos=vec(-1,0.1,0), size = vec(0.4,0.2,0.1), color = color.black, make_trail=True)

ball = box ( pos = anchor.pos + vec(L,0,0), v = vec(0,0,0), m = mass, size = vec(0.2,0.2,0.1), color = color.red, make_trail = True , retain = 60)
ball.p = ball.v*ball.m
ball.v = vec(0,0,0)

spring = helix( pos = anchor.pos, axis = ball.pos - anchor.pos, radius = 0.05 , color = color.orange )



vgraph = graph(title = 'velocity' , xtitle = 't', ytitle = 'v(t)' )
vx_s = gcurve(graph = vgraph, color = color.cyan)

fgraph = graph(title = 'Force' , xtitle = 't', ytitle = 'N' )
fscurve = gcurve (graph = fgraph, color = color.orange)
ffcurve = gcurve(graph = fgraph, color = color.red)

T = 0
t = 0
dt = 0.01

ball.T = 0
ball.old_v = ball.v

while t < 15:
    rate(200)
    
    L = ball.pos-anchor.pos
    
    Ff = -mu*(mass*g)*ball.v.hat
    Fs = -k * (L.mag - L_0) * L.hat 
    F = Ff+Fs
    
    Vold = ball.v
    
    ball.p = ball.p + F*dt
    ball.v = ball.p/ball.m
    ball.pos = ball.pos + ball.v*dt
    
    spring.axis = ball.pos - anchor.pos
    
    vx_s.plot(t,ball.v.x)
    
    fscurve.plot(t,Fs.x)
    ffcurve.plot(t,Ff.x)
    
    Vnew = ball.v
    
    if Vold.x>0 and Vnew.x<0: 
        print("period",ball.T)
        ball.T = 0
        
    
    t = t + dt
    
    ball.T = ball.T + dt






Editor is loading...
Leave a Comment