Skip to content

Commit

Permalink
Update Pendul.py to Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ejuarezg committed Aug 4, 2019
1 parent fa9a14f commit 0351fe4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Python/Raw Python/Pendul.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
import matplotlib.pyplot as plt

#* Select the numerical method to use: Euler or Verlet
NumericalMethod = input('Choose a numerical method (1: Euler; 2: Verlet): ')
NumericalMethod = int(input('Choose a numerical method (1: Euler; 2: Verlet): '))

#* Set initial position and velocity of pendulum
theta0 = input('Enter initial angle (in degrees): ')
theta0 = float(input('Enter initial angle (in degrees): '))
theta = theta0 * np.pi /180 # Convert angle to radians
omega = 0.0 # Set the initial velocity

#* Set the physical constants and other variables
g_over_L = 1.0 # The constant g/L
time = 0.0 # Initial time
irev = 0 # Used to count number of reversals
tau = input('Enter time step: ')
tau = float(input('Enter time step: '))

#* Take one backward step to start Verlet
accel = -g_over_L * np.sin(theta) # Gravitational acceleration
theta_old = theta - omega*tau + 0.5*accel*tau**2

#* Loop over desired number of steps with given time step
# and numerical method
nstep = input('Enter number of time steps: ')
nstep = int(input('Enter number of time steps: '))
t_plot = np.empty(nstep)
th_plot = np.empty(nstep)
period = np.empty(nstep) # Used to record period estimates
Expand All @@ -51,7 +51,7 @@
#* Test if the pendulum has passed through theta = 0;
# if yes, use time to estimate period
if theta*theta_old < 0 : # Test position for sign change
print 'Turning point at time t = ',time
print('Turning point at time t = ',time)
if irev == 0 : # If this is the first change,
time_old = time # just record the time
else:
Expand All @@ -63,7 +63,7 @@
nPeriod = irev-1 # Number of times the period was measured
AvePeriod = np.mean( period[0:nPeriod] )
ErrorBar = np.std(period[0:nPeriod]) / np.sqrt(nPeriod)
print 'Average period = ', AvePeriod, ' +/- ', ErrorBar
print('Average period = ', AvePeriod, ' +/- ', ErrorBar)

# Graph the oscillations as theta versus time
plt.plot(t_plot, th_plot, '+')
Expand Down

0 comments on commit 0351fe4

Please sign in to comment.