Simple pendulum with VPython

A simple pendulum is a classic physics system that consists of a mass attached to a fixed point by a string or rod of a fixed length.

When the pendulum is displaced from its equilibrium position and released, it oscillates back and forth due to the force of gravity, creating a periodic motion called a simple harmonic motion (SHM).

Mathematical equations for a simple pendulum

Suppose a pendulum bob of mass mm is attached to a string of length LL. When the bob is undergoing SHM, the vertical and horizontal components its weight (mgmg) will be mg  sinθ and mg  cosθmg\;sin\theta \textnormal{ and } mg\;cos\theta.

A simple pendulum
A simple pendulum

Since the pendulum is undergoing a semi-circular motion, it must have angular components as follows:

  • Angular acceleration: The main postulate of SHM states that the angular acceleration of the pendulum is always towards the center (and hence negative). It is given by:

where α\alpha is the angular acceleration.

  • Angular velocity: Given the α\alpha, the angular velocity(ω\omega) will be:

where tt is the pendulum's instantaneous time.

  • Angular displacement: Using the similar principle above, the angular displacement (θ\theta) will be:

Code

We can simulate the simple pendulum system using the VPython library. To execute the provided code, you have to follow below mentioned steps:

Once you click the "Run" button, follow the encircled link
1 of 4

The sample code is as follows:

from vpython import *

# Constants
g = 9.81  # Acceleration due to gravity (m/s^2)
L = 2.0   # Length of the pendulum (m)
theta0 = pi / 4  # Initial angle (radians)
omega0 = 0.0     # Initial angular velocity (radians/s)
dt = 0.01       # Time step for simulation (s)

# Create objects
ceiling = box(pos=vector(0, 0, 0), size=vector(0.2, 0.2, 0.2), color=color.red)
bob = sphere(pos=vector(L * sin(theta0), -L * cos(theta0), 0), radius=0.1, color=color.blue)
string = cylinder(pos=ceiling.pos, axis=bob.pos - ceiling.pos, radius=0.05, color=color.green)

# Initial conditions
theta = theta0
omega = omega0

# Simulation loop
while True:
    rate(100)  # Set the frame rate to 100 frames per second

    # Calculate the acceleration and update angular velocity and angle
    alpha = -(g / L) * sin(theta)
    omega += alpha * dt
    theta += omega * dt

    # Update the position of the pendulum bob
    bob.pos = vector(L * sin(theta), -L * cos(theta), 0)

    # Update the string
    string.axis = bob.pos - ceiling.pos

Code explanation

  • Line 1: Importing the VPython library

  • Lines 4–8: Storing the necessary constants. g is the gravitational acceleration, L is the length of the string attached to the pendulum bob, theta0 is the initial angle by which the pendulum is pulled, omega0 is the initial angular velocity, and dt is the instantaneous time of SHM.

  • Line 11: ceiling variable stores a box object, which creates a squared pivot point to which the pendulum string is attached.

  • Line 12: bob variable stores a sphere object, which creates a blue-colored pendulum bob attached to the other end of the string.

  • Line 13: string variable stores a cylinder object, which creates the string to which the pendulum bob is attached.

  • Line 16–17: Initial values of θ\theta and ω\omega are getting stored in the theta and omega variables respectively.

  • Line 20: The while loop here will keep executing until the vertical displacement gets zero, i.e., the ball hits the ground.

  • Line 21: Sets frames per second.

  • Lines 24–26: Angular acceleration, angular velocity, and angular displacement are updated according to the abovementioned equations.

  • Lines 29–32: Depending upon the updated angular displacement, bob's and string's position vector is being changed.

Note: Learn how to simulate projectile motion in VPython.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved