Section 7: Pendulum Period Calculator#
Adapted from: “Introduction to Programming Using Fortran 95/2003/2008” by Ed Jorgensen (March 2018 / Version 3.0.51).
Program to Calculate the Period of a Pendulum#
program period
! Program to calculate the period of a pendulum
! Declare variables
! real constants -> gravity, pi
! reals -> angle, length, alpha
implicit none
real :: angle, length, pperiod, alpha
real, parameter :: gravity = 980.0, pi = 3.14159
! display initial header
write (*,'(a)') "Pendulum Period Calculation Program"
print *
! prompt for and read the length of the angle values
! for the website, we will hard code the length and angle
!write (*,'(a)', advance="no") "Enter the Length and Angle values: "
!read (*,*) length, angle
length = 1 ! meters
angle = 10 ! degrees
write (*, '(a, f8.2, a)') "The Length is: ", length, " meters"
write (*, '(a, f8.2, a)') "The Angle is: ", angle , " degrees"
! convert degrees to radians
alpha = angle * pi / 180.0
! calculate the period
pperiod = 2.0 * pi * sqrt(length / gravity) * ( 1.0 + 1.0/4.0 * sin( alpha / 2.0 )**2)
! display the results
print *
write (*, '(a)') "Calculating Results......"
write (*,'(a, f8.2, a)') "The period is: ", pperiod, " seconds."
print *
end program period
Program Explanation#
The following explanation is given by “Code Copilot” within ChatGPT 4.
The Fortran program provided is designed to calculate the period of a pendulum based on its length and the angle of displacement. Below is a detailed explanation of the program:
Structure Overview#
Program Name:
period
Purpose: Calculates the period of a pendulum using the small-angle approximation for simple harmonic motion.
Variable Declaration#
angle: The initial angle of the pendulum swing (in degrees).
length: The length of the pendulum (in meters).
pperiod: Calculated period of the pendulum’s oscillation (in seconds).
alpha: Angle in radians, used in the period calculation formula.
gravity and pi: Constants representing the acceleration due to gravity (cm/s²) and the mathematical constant Pi, respectively.
Program Flow#
Header Display: The program starts by displaying a header “Pendulum Period Calculation Program” to indicate its function.
Input Handling:
In a typical scenario, the program might prompt the user to enter the length and angle. However, for demonstration or specific usage (e.g., on a website), these values are hardcoded with:
length = 1
meterangle = 10
degrees
It then displays these values back to the user.
Angle Conversion:
The angle input in degrees is converted into radians. This is necessary because trigonometric functions in programming typically expect angles in radians.
Conversion formula:
alpha = angle * pi / 180.0
Period Calculation:
The formula used for calculating the period of a pendulum under the small-angle approximation is:
\( \Large T = 2\pi \sqrt{\frac{\text{length}}{\text{gravity}}} \left(1 + \frac{1}{4} \sin^2\left(\frac{\alpha}{2}\right)\right) \)
This formula considers the correction for larger angles using the first term of the Taylor series expansion of the sine function.
Result Display:
The calculated period of the pendulum is displayed in seconds, formatted to two decimal places.
Final Output#
The program outputs the formatted results of the pendulum’s period calculation, providing a straightforward demonstration of basic physics calculations in Fortran.
This program exemplifies good practices in Fortran programming, including clear variable naming, modular steps in calculations, and output formatting. However, note the potential unit discrepancy with the gravity constant, which could lead to incorrect results unless adjusted or documented properly.
Program Compilation and Execution#
The above program is compiled and run using Fortran Package Manager (fpm):
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_7_Period_Calculator"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")
exec_status = os.system("fpm run 2>/dev/null")
Pendulum Period Calculation Program
The Length is: 1.00 meters
The Angle is: 10.00 degrees
Calculating Results......
The period is: 0.20 seconds.