Period in the real pendulum

Jonathon Sumner
Physics Department
Dawson College
Jean-François Brière
Physics Department
Dawson College
Sameer Bhatnagar
Physics Department
Dawson College
March 13, 2019
March 15, 2019

Studying the pendulum beyond the small angle approximation.

You learned that the period of a pendulum was independent from its amplitude. This was true under the small angle approximation. What can we say about the period when we go beyond that approximation?

Lab instructions

Real pendulum lab instructions

Code template

Pendulum trajectory code (student version)

Code sample: Real pendulum code template
/**
Motion of a pendulum and variation in period as a function of amplitude.

Author: J.-F. Briere
Current version written: March 2019
Description: Calculates the motion of a pendulum as a function of time and extract the period from the data.

NOTE: You need the package jmathplot to run the code.
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import org.math.plot.*;
import org.math.plot.plotObjects.*;

public class pendulumTrajectoryStudent
{
  	// Declare constants (think of what will be constant through the code, mass for instance)
  	// Remember to put units!
  	public static final double dt = 1e-3;    	// time interval in seconds
  	public static final double tMax = 20.;    	// duration of simulation in seconds
	public static final double G = 9.807;    	// acceleration due to gravity in m/s/s
	public static final double L = 1.0;			// Length of pendulum in meter
	public static final	double amplitude = 0.5; // initial amplitude in radians

	public static final boolean plotAngleVsTime = true; // Only store and plot the angle vs time graph when set to true

	// Start main method
 	public static void main(String[] args)
  	{
		////////////////////////////////////////////////////////////////
		//1. Opening a file to store the angle vs time data
		///////////////////////////////////////////////////////////////

		// Open log file to write the angle vs time data
  	  	PrintWriter outputFile = null;
  	  	try
  	  	{
  	  	  outputFile = new PrintWriter(new FileOutputStream("angle_vs_time.txt",false));
  	  	}
  	  	catch(FileNotFoundException e)
  	  	{
  	  	  System.out.println("File error.  Program aborted.");
  	  	  System.exit(0);
  	  	}

		//////////////////////////////////////////////////////////////////////
		//2. Calculating angle as a function of time
		//////////////////////////////////////////////////////////////////////

		// Calculate length of arrays
		int imax = (int)(tMax/dt);		// Maximal index

		// Declare main arrays
		double[] t = new double[imax];		// time in sec
		double[] angle = new double[imax];	// angular position in radians
		double angVelInitial = 0;	// initial angular velocity in rad/s

		/////////////////////////////////////////////////////////////////////
		// Applying Verlet's method to solve the ODE (get angle vs time)
		/////////////////////////////////////////////////////////////////////

		// Initialize time
		t[0] = 0;

		// We start the pendulum from rest and with its angular position equal to the amplitude
		angle[0] = amplitude;

		// Get the initial angular acceleration by calling the angAcceleration method

		// Remember, for Verlet, we need the previous two points so we
		// do one step of Euler to get value for i=1
		t[1] = t[0]+dt;
		angle[1] = angle[0] + angVelInitial*dt;

		for(int i=2;i<=imax;i++)
	    {
			// Update time
			t[i] = t[i-1]+dt;

			// TASK B2: Rewrite the next line using Verlet's method
		  angle[i] = 0;

		}

		//////////////////////////////////////////////////////////////////////////////
		// 3. Print and plot angle as a function of time
		//////////////////////////////////////////////////////////////////////////////
		if (plotAngleVsTime)
	  	{
			//Storing info in a file to use later if needed
			for(int i=0;i<(imax-2);i+=10) // Only print every 10 points
			{
				outputFile.println(t[i] + "	" + angle[i]);
			}

	   		plot(amplitude, t, angle); //Method below
		}

		//////////////////////////////////////////////////////////////////////////////
		// 4. Find and then print period as a function of amplitude
		//////////////////////////////////////////////////////////////////////////////
		System.out.println("Amplitude	Period");
		System.out.println("(rad)		(s)");
		System.out.println(amplitude + "		" + findPeriod(t,angle));

		///////////////////////////////////////////////////////////////////////
		// 5. Closing file
		///////////////////////////////////////////////////////////////////////
		outputFile.close();

	}//main

	////////////////////////////////////////////////////////////////////////////
	// 6. Method calculating the angular acceleration
	////////////////////////////////////////////////////////////////////////////
	// TASK B1: Write the methode calculating the angular acceleration
	// 			Your method should have:
	//				the angle as a parameter, and
	//				should return the angular acceleration


  	///////////////////////////////////////////////////////////////////////
  	// 7. Live graphing of angular postion as a function of time for amplitude = printamplitude
  	///////////////////////////////////////////////////////////////////////
  	public static void plot(double amp, double[] time, double[] angle)
  	{
  	  	Plot2DPanel plot1 = new Plot2DPanel();

    	// define the legend position
    	plot1.addLegend("SOUTH");

    	// add a line plot to the PlotPanel
    	plot1.addLinePlot("Pendulum 1", time, angle);
    	plot1.setAxisLabel(0,"time (s)");
    	plot1.setAxisLabel(1,"angle (rad)");
    	BaseLabel title1 = new BaseLabel("Angular position v.s. time for an amplitude of "
    	  + amp + " rad", Color.BLACK, 0.5, 1.1);

    	title1.setFont(new Font("Courier", Font.BOLD, 14));
    	plot1.addPlotable(title1);

    	// put the PlotPanel in a JFrame like a JPanel
    	JFrame frame1 = new JFrame("a plot panel");
    	frame1.setSize(600, 600);
    	frame1.setContentPane(plot1);
    	frame1.setVisible(true);
  	}//plot

  	///////////////////////////////////////////////////////////////////////
  	// 8. Calculating and returning the period
  	///////////////////////////////////////////////////////////////////////
  	public static double findPeriod(double[] time, double[] angle)
  	{
  	  	// BONUS TASK (ONLY DO IF YOU'RE DONE WITH EVERYTHING)
  	  	// Find a better way to get the period from the angle data.

    	// Look for two consecutive times when the angle is zero. That correspond to half a cycle.
    	int j = 0; 		 // Index looping over time
    	double angleIsZero = 0; // Storing the time when angle is zero

    	while(angleIsZero == 0 && j < angle.length-1)
    	{
    	  	// If the angle switch sign
    	  	if(angle[j]*angle[j+1]<0)
    	  	{
    	    // Choose the angle with the smallest magnitude
        	angleIsZero = Math.abs(angle[j]) < Math.abs(angle[j+1]) ? time[j]:time[j+1];
      		}
      	j++;
    	}

    	double angleIsZeroNext = 0; // Storing the next time when angle is zero

    	while(angleIsZeroNext == 0 && j < angle.length-1)
    	{
      		// If the angle switch sign
      		if(angle[j]*angle[j+1]<0)
      		{
        	// Choose the angle with the smallest magnitude
        	angleIsZeroNext = Math.abs(angle[j]) < Math.abs(angle[j+1]) ? time[j]:time[j+1];
      		}
      	j++;
    	}

    	// The 2* below is needed because we have the time difference for half a cycle.
    	return 2*(angleIsZeroNext-angleIsZero);
  	} //findPeriod

} //class

Library

jmathplot library