Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.5 kB
4
Indexable
Never
Web VPython 3.2
scene = canvas(background = color.white)

L_0 = 8
theta_0 = 10 * pi/180
omega_0 = 0 
g = 9.8

fixed_point = sphere(pos=vec(0,6,0), radius=0.5, color=color.red)

ball = sphere(pos = fixed_point.pos + vec(L_0 * sin(theta_0), -L_0 * cos(theta_0), 0), mass = 0.5, radius = 0.2, color = color.blue)

spring = helix(pos = fixed_point.pos, axis = ball.pos - fixed_point.pos, radius = 0.2, color = color.orange)


#Angular Displacement graph
a_graph = graph(title = "Theta - Omega", xtitle = "t", ytitle = " ")

#Velocity curve
theta_plot = gcurve(graph = a_graph, color = color.red)
omega_plot = gcurve(graph = a_graph, color = color.green)

ball.omega = omega_0
ball.p = ball.mass * L_0 * ball.omega

ball.theta = theta_0

t = 0
dt = 0.001
myrate = 2500

#Stores old omega
ball.omega_old = ball.omega
#Period tracker
ball.T = 0 

scene.waitfor('click')

while t < 20:
    rate(myrate)
    spring.axis = ball.pos - fixed_point.pos
    
    F = -ball.mass * g * sin(ball.theta)
    
    ball.p = ball.p + F*dt
    ball.omega = ball.p / (L_0 * ball.mass)
    ball.theta = ball.theta + ball.omega * dt
       
    ball.pos = fixed_point.pos + vec(L_0*sin(ball.theta), -L_0*cos(ball.theta), 0)
    
    theta_plot.plot(pos = (t, ball.theta))
    omega_plot.plot(pos = (t, ball.omega))
    
    t += dt
    
    if ball.omega_old > 0 and ball.omega < 0:
        print(ball.T, 2*pi*sqrt(L_0/g))
        ball.T = 0
        
    ball.omega_old = ball.omega
    ball.T += dt
    
    







Leave a Comment