Determine the Gravity Acceleration from a Pendulum Experiment

In this example, we wish to determine the local acceleration due to gravity g based on an experiment in which we measure the period of a pendulum. The program will serve as a paradigm for our programming exercise sequence.

(Source: http://limerickdb.com/?top150 - Limerick #106)

The complete development of this well known result is presented in pend_eqns.html. In the following we wish to use this equation to determine the local acceleration due to gravity g based on a pendulum experiment. Solving for g we obtain:

This leads to the following structure diagram:

The complete program follows. Notice in particular the Constructor of the class Pendulum, and the interaction between the main function and the Object pend.

// PURPOSE: to evaluate g[m/s^2] as a function of pendulum period
// FILENAME: gfun.cpp
// PROGRAMMER: Dr.Iz (Urieli) 8/11/03
// DESCRIPTION: class Pendulum defines the pendulum in terms
//   of its length and baseline value of g. A method is defined
//   in the class to determine the local value of g given the
//   measured value of period.
#include <iostream>
#include <cmath>
using namespace std;

float const PI = 3.141593;
class Pendulum{
 private:
   float length; // [m] pendulum length
 public:
   float g_base; // [m/s^2] baseline value of g
   Pendulum(float x = 1.0) { // Constructor for Pendulum
      g_base = 9.807; // [m/s^2]
      length = x;
      cout << "pendulum length set at " << length << "[m]\n";
      cout << "for baseline gravity of " << g_base << "[m/s^2]\n";
      cout << "small angle period is "
           << 2*PI*sqrt(length/g_base) << "[s]\n"; 
   }
   // function to determine local acceleration due to gravity g
   //     based on a pendulum experiment
   // pre: argument "period" and pendulum "length" have values
   // post: value of g has been evaluated and returned.
   float find_g(float period) {
      float g = 4*PI*PI*length/(period*period);
      return g; // [m/s^2]
   }
};

int main() {
   float length, period;
   cout << "This program evaluates the local value of g[m/s^2]\n"
        << "as a function of pendulum period\n";
   cout << "\nEnter the pendulum length [m]: ";
   cin >> length;
   Pendulum pend(length); // creating the Pendulum object "pend"
   cout << "enter period [s]: ";
   cin >> period;
   cout << "for period = " << period << " seconds, "
        << "g = " << pend.find_g(period) << "[m/s^2]\n";
   return 0;
}

We find that in C++ programming one usually separates the program into a .cpp (dot-cpp) file and a .h (dot-h) file, and we will do so in all the exercises of the programming sequence. Thus we have separated the gfun.cpp program file shown above into gfun1.cpp, and gfun1.h.

The program gfun.cpp is found in the week3 directory in condor, together with the two derived files gfun1.cpp and gfun1.h.

Telnet: "condor"