Skip to content

Instantly share code, notes, and snippets.

@maciejczyzewski
Created April 12, 2016 21:45
Show Gist options
  • Save maciejczyzewski/c8d822f8bc0bf5b30d1f94c8c3ca90ba to your computer and use it in GitHub Desktop.
Save maciejczyzewski/c8d822f8bc0bf5b30d1f94c8c3ca90ba to your computer and use it in GitHub Desktop.
Measuring Gravity with a Pendulum.
import numpy as np
import pandas as pd
df = pd.read_csv('grav.csv')
pd.set_option('display.width', 1000)
def t_avg(row):
return sum(row[0:4]) / 4
df['t_avg'] = df.apply(lambda row: t_avg(row), axis=1)
def t_err(row):
t_max = max(row[0:4]); t_min = min(row[0:4])
return (t_max - t_min) / 2
df['t_err'] = df.apply(lambda row: t_err(row), axis=1)
def l_avg(row):
return sum(row[4:7]) / 3
df['l_avg'] = df.apply(lambda row: l_avg(row), axis=1)
def l_err(row):
l_max = max(row[4:7]); l_min = min(row[4:7])
return (l_max - l_min) / 2
df['l_err'] = df.apply(lambda row: l_err(row), axis=1)
def g_avg(row):
L = (row['l_avg'] + 1) / 100
T = row['t_avg'] / 10
return (4 * ((3.14) ** 2) * L) / (T ** 2)
df['g_avg'] = df.apply(lambda row: g_avg(row), axis=1)
def g_err(row):
L = (row['l_avg'] + 1) / 100
L_err = row['l_err'] / 100
T = row['t_avg'] / 10
T_err = row['t_err'] / 10
return row['g_avg'] * ((L_err / L) + 2 * (T_err / T))
df['g_err'] = df.apply(lambda row: g_err(row), axis=1)
g = sum(df['g_avg']) / 3; g_err = sum(df['g_err']) / 3
p = sum(df['g_avg'][1:]) / 2; p_err = sum(df['g_err'][1:]) / 2
print df
print "=== RESULT ==="
print "g = {0} +- {1} m/s^2".format(g, g_err)
print "g = {0} +- {1} m/s^2 (optimal)".format(p, p_err)
t0 t1 t2 t3 l0 l1 l2
20.40 20.48 20.11 20.55 98 101 99
24.77 24.72 24.72 24.41 149 151 150
28.56 28.52 28.57 28.48 201 198 201
@maciejczyzewski
Copy link
Author

      t0     t1     t2     t3   l0   l1   l2    t_avg  t_err       l_avg  l_err     g_avg     g_err
0  20.40  20.48  20.11  20.55   98  101   99  20.3850  0.220   99.333333    1.5  9.522327  0.347895
1  24.77  24.72  24.72  24.41  149  151  150  24.6550  0.180  150.000000    1.0  9.796845  0.207928
2  28.56  28.52  28.57  28.48  201  198  201  28.5325  0.045  200.000000    1.5  9.737236  0.103380
=== RESULT ===
g = 9.68546923148 +- 0.219734502545 m/s^2
g = 9.76704028632 +- 0.155654240402 m/s^2 (optimal)

Location: Szczecin, Poland

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment