Skip to content

Instantly share code, notes, and snippets.

@Dudemanguy
Created October 17, 2017 18:57
Show Gist options
  • Save Dudemanguy/be8d1c0c60bd20ae554562ed47cb90ef to your computer and use it in GitHub Desktop.
Save Dudemanguy/be8d1c0c60bd20ae554562ed47cb90ef to your computer and use it in GitHub Desktop.
Python script for calculating the value of g using Krater's Pendulum as well as propagated error.
import math
import numpy as np
import sys
filename = sys.argv[-1]
fin = open(filename,'r')
L1 = fin.readline()
L2 = fin.readline()
T1 = fin.readline()
T2 = fin.readline()
#L1 = input('Input L1 values in meters. \n')
#L2 = input('Input L2 values in meters. \n')
#T1 = input('Input T1 values. \n')
#T2 = input('Input T2 values. \n')
#split input into separate floats
L1_n = [float(x) for x in L1.split()]
L2_n = [float(x) for x in L2.split()]
T1_n = [float(x) for x in T1.split()]
T2_n = [float(x) for x in T2.split()]
#average values
L1_m = np.mean(L1_n)
L2_m = np.mean(L2_n)
T1_m = np.mean(T1_n)
T2_m = np.mean(T2_n)
g = ((4*math.pi**2)*(L1_m**2 - L2_m**2))/(T1_m**2*L1_m - T2_m**2*L2_m)
#error derivatives
L1_d = (4*math.pi**2 * (L1_m**2 * T1_m**2 - L1_m * L2_m * T2_m**2 + L2_m**2 * T1_m**2)/(L1_m*T1_m**2 - L2_m*T2_m**2)**2)
L2_d = (4*math.pi**2 * (L1_m**2 * T2_m**2 - L1_m * L2_m * T1_m**2 + L2_m**2 * T2_m**2)/(L1_m*T1_m**2 - L2_m*T2_m**2)**2)
T1_d = (-8*math.pi**2 * L1_m * T1_m * (L1_m**2 - L2_m**2))/(L1_m*T1_m**2 - L2_m*T2_m**2)**2
T2_d = (8*math.pi**2 * L2_m * T2_m * (L1_m**2 - L2_m**2))/(L1_m*T1_m**2 - L2_m*T2_m**2)**2
#standard deviations
T1_error = np.std(T1_n)
T2_error = np.std(T2_n)
L_error = np.std(L2_n)
#final error
g_error = ((L1_d * L_error)**2 + (L2_d * L_error)**2 + (T1_d * T2_error)**2 + (T2_d * T2_error)**2)**0.5
percent_error = 100*(9.79281 - g)/(9.79281)
print("Std of L1, T1, and T2 are")
print(L_error, T1_error, T2_error)
print('The calculated value of g is')
print(g)
print('The error of g is')
print(g_error)
print('The percent error')
print(percent_error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment