VPython makes it easy to create visualizations and animations for physics experiments. This program will simulate a pendulum and visualize its motion.
First, make sure you have VPython installed. You can install it using pip:
pip install vpython
Then, you can use the following code to create the pendulum simulation:
from vpython import *
# Constants
length = 1.0 # Length of the pendulum (meters)
g = 9.8 # Acceleration due to gravity (m/s^2)
theta = 0.2 # Initial angle (radians)
omega = 0.0 # Initial angular velocity (radians/s)
# Time parameters
dt = 0.01 # Time step (seconds)
t = 0.0 # Initial time
# Setting up the scene
scene = canvas(title='Pendulum Simulation',
width=800, height=600,
center=vector(0, -length/2, 0),
background=color.white)
# Creating the pendulum components
pivot = vector(0, 0, 0) # Pendulum pivot point
bob = sphere(radius=0.05, color=color.blue) # Pendulum bob
rod = cylinder(pos=pivot, axis=vector(length*sin(theta), -length*cos(theta), 0),
radius=0.01, color=color.black) # Pendulum rod
# Function to update the pendulum position
def update_pendulum():
global theta, omega, t
# Calculating angular acceleration
alpha = - (g / length) * sin(theta)
# Updating angular velocity and angle
omega += alpha * dt
theta += omega * dt
# Updating bob and rod positions
bob.pos = vector(length*sin(theta), -length*cos(theta), 0)
rod.axis = bob.pos - pivot
# Updating time
t += dt
# Main loop
while True:
rate(100) # Limit the simulation to 100 updates per second
update_pendulum()
Edited by
caa on 03-01-2026 21:24,
3 days ago