lab7_FINISHED

 avatar
unknown
python
8 days ago
1.0 kB
4
Indexable
Web VPython 3.2
from visual import *

# Constants
L0 = 8
theta0 = 10 * pi/180
g = 9.8
m = 5
t = 0
dt = 0.01

anchor = sphere(pos=vec(0,0,0), radius=0.2, color=color.yellow)
ball = sphere(pos=anchor.pos + vec(0,-L0,0), color=color.red, mass=m, v=vec(0,0,0), radius=0.5)
string = helix(pos=anchor.pos, axis=ball.pos - anchor.pos, radius=0.2, color=color.orange)

ball.theta = theta0
ball.omega = 0
ball.p = 0

graph1 = graph(title="Pendulum Motion", xtitle="Time (s)", ytitle="Values", fast=False)
theta_curve = gcurve(color=color.blue, label="Theta (rad)")
omega_curve = gcurve(color=color.red, label="Omega (rad/s)")

while t < 10:
    rate(150)
    
    F = m * -g * sin(ball.theta)
    
    ball.p = ball.p + F * dt
    ball.omega = ball.p / ball.mass
    ball.theta = ball.theta + ball.omega * dt
    
    ball.pos = anchor.pos + vec(L0 * sin(ball.theta), -L0 * cos(ball.theta), 0)
    string.axis = ball.pos - anchor.pos
    
    theta_curve.plot(t, ball.theta)
    omega_curve.plot(t, ball.omega)
    
    t += dt
Editor is loading...
Leave a Comment