Untitled
unknown
plain_text
5 months ago
955 B
15
Indexable
Web VPython 3.2 scene.width = 600 scene.y = 400 scene.title = "Mass-Spring System on Y-Axis" vgraph = gcurve(color=color.green) mass = sphere(pos=vector(0, -0.95, 0), radius=0.3, color=color.cyan) m_mass = 1 # kg spring_constant = 1 # N/m initial_position = 10 # m initial_velocity = 0 # m/s p_mass = m_mass * vector(0, initial_velocity, 0) delta_t = 0.01 t = 0 oscillations = 0 previous_direction = 1 if p_mass.y > 0 else -1 for _ in range(10000): # N rate(2000) # rate mass.pos.y += p_mass.y * delta_t / m_mass t += delta_t F_spring = -spring_constant * (mass.pos.y - initial_position) p_mass += vector(0, F_spring, 0) * delta_t current_direction = 1 if p_mass.y > 0 else -1 if current_direction != previous_direction: oscillations += 1 previous_direction = current_direction vgraph.plot(pos=(t, p_mass.y/m_mass)) print(f"Elapsed time: {t:.2f} seconds") print(f"Oscillations: {oscillations / 2}")
Editor is loading...
Leave a Comment