Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Member Polls
whats your favorite language

Python program using VPython to simulate a pendulum

Last updated on 4 days ago
C
caaSuper Admin
Posted 4 days ago
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
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 5
Members Online 0

Total Members: 21
Newest Member: brijamohanjha