Modelling a Pendulum
This project’s aim was to help me learn how to analyse data using Python: a skill that is very important in real research. I was able to model the path of a pendulum using Python code.
Setting up the Pendulum
Experimental Setup and Contrast Optimization
To investigate pendulum motion, a simple pendulum system was constructed by suspending a compact mass of light-blue Blu-Tack from a secure anchor point using a length of string, which was initially kept as an unmeasured variable. High-speed video recordings were captured to track the oscillations of the pendulum over time. However, during the initial video review, a clear experimental challenge emerged: the light-blue coloration of the Blu-Tack provided poor contrast against the white background wall, making precise position tracking difficult and introducing potential tracking errors. To resolve this issue and optimize the optical clarity of the data, a dark-blue marker was used to heavily color the surface of the Blu-Tack. This adjustment significantly enhanced the chromatic contrast against the background, ensuring distinct visibility and high tracking fidelity for subsequent frame-by-frame analysis.
How did I model the pendulum’s motion?
Digital Motion Tracking Analysis
To analyze the mechanics of the pendulum precisely, automated video tracking software was utilized to extract spatial coordinates from the high-speed footage. By locking onto the high-contrast, dark-blue colored bob, the program automatically recorded the exact position of the pendulum frame-by-frame. Utilizing computational tracking significantly minimized human measurement error and saved an immense amount of time compared to manual frame analysis, yielding a highly accurate, clean dataset of horizontal displacement over time.
Mathematical Modeling via Curve Fitting
Once the time and position data points were exported, a Python script was developed to find the unknown length of the string using non-linear least squares regression. Because an ideal pendulum undergoes simple harmonic motion, its lateral displacement follows a sinusoidal path described by the mathematical model:
x(t) = Asin(ωt+ϕ) + C
By fitting this theoretical sine curve to the experimental tracking data, the program extracts the optimized angular frequency (ω), which directly relates to the period of oscillation and the local acceleration due to gravity (g = 9.81 ms^-2).
Defining the Target Function and Initial Parameter Guesses
The python program initializes by defining the standard sinusoidal function template: def sineFunc(time, A, w, phi, const):. This function accepts the independent time variable along with four key tuning parameters: amplitude (A), angular frequency (ω), phase shift (ϕ), and a vertical offset constant (C). Because optimization algorithms can get stuck in mathematical errors if left to hunt blindly, a list called guessParams is populated with initial, rough estimations—such as setting A_guess = 200 and calculating an estimated angular frequency w_guess = 2 np.pi 0.5—giving the optimization function a steady baseline starting point.
Parameter Optimization and Unpacking
The optimization core relies on SciPy's curve-fitting algorithm through the line popt, = curvefit(sineFunc, time, position, guessParams). This function automatically alters the parameters iteratively until the residual difference between the raw tracking data points and the theoretical sine wave is completely minimized. The optimized values are saved into the popt array and immediately unpacked into individual variables: A_optimized, w_optimized, phi_optimized, and const_optimized. These represent the true physical parameters of the experimental pendulum system.
Plotting and Visual Verification
To visually verify the precision of the fit, the script generates an interactive figure utilizing three distinct plotting commands. First, it plots the raw tracked position coordinates as isolated marker points (marker = 'o'). Next, it overlays a green dashed line representing the initial parameter guess curve, followed by a solid line showing the final, optimized sine curve. Displaying these curves simultaneously via plt.show() provides an immediate visual confirmation of the script's mathematical accuracy, showing how perfectly the final optimized model hugs the actual physical data tracks.
Calculating String Length via Pendulum Mechanics
The final phase of the script applies the physical laws of classical mechanics to isolate the string length. Using the optimized angular frequency (ω), the period of the pendulum (T) is derived using:
T = 2π/ω
In Python, this is executed via timePeriod = 2*np.pi/w_optimized. According to the small-angle approximation for a simple pendulum, the period is governed by the formula:
T = 2π root(L/g)
By algebraically rearranging this equation to isolate the length (L), the script computes length = gravity (timePeriod / (2np.pi))2. Finally, the program outputs the result using print("Length of cable = ", length*100), scaling the final value by 100 to display the calculated length of the string cleanly in centimeters.