Untitled
LAB7_Finishedunknown
python
8 months ago
1.5 kB
19
Indexable
Web VPython 3.2
from visual import *
# Constants
L0 = 8
theta0 = 10 * pi/180
g = 9.8
m = 5
dt = 0.01
t = 0
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)")
# Theoretical period
T_theoretical = 2 * pi * sqrt(L0 / g)
T_numerical = 0
first_crossing = False
start_time = 0
while t < 10:
rate(150)
F = m * -g * sin(ball.theta)
ball.p = ball.p + F * dt
ball.omega = ball.p / (ball.mass * L0)
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
# Update graphs
theta_curve.plot(t, ball.theta)
omega_curve.plot(t, ball.omega)
if ball.theta > 0 and not first_crossing:
first_crossing = True
start_time = t
elif ball.theta < 0 and first_crossing:
T_numerical = (t - start_time) * 2 # Full oscillation
first_crossing = False
t += dt
print("Theoretical Period:", T_theoretical, "s")
print("Numerical Period:", T_numerical, "s")Editor is loading...
Leave a Comment