def get_data(cls, fname): data = pd.read_excel(fname, 'angle_calibration') delta_V = data['delta V (uV)'] * 1e-6 delta_V_err = np.ones_like(delta_V) * 2 * 1e-6 phi = data['dPhi Real (degree)/25'] / 25 * np.pi / 180 V_A = data['V_a (mV)'] * 1e-3 V_A_err = np.ones_like(V_A) * 2e-6 va = Value(V_A, V_A_err) dv = Value(delta_V, delta_V_err) return phi, va, dv
def __init__(self, data_file): self.g = Value(9.8058, 2/1e5) self.raw_data = self._load_raw(data_file) self.analysis = None self.I = None self.m = None
def get_BL(self): slope = self.get_slope() g = self.g bl = g.value/slope.value blerr = np.sqrt((g.err/slope.value)**2 + (slope.err*g.value/slope.value**2)**2) return Value(bl, blerr, 'kgm/s^2A')
def get_k(self): m = np.abs(self.analysis.model.params[0]) merr = self.analysis.model.param_errs[0] v = 1 / m e = merr * 1 / m**2 return Value(v, e)
def _f_phi_measured(self, dv, va): v = 1 / 2 * dv.v / (2 * va.v - dv.v) e = [ 1 / 4 * (dv.e * (2 * va.v / (2 * va.v - dv.v)**2))**2, 1 / 4 * (va.e * (2 * dv.v / (2 * va.v - dv.v)**2))**2 ] return Value(v, np.sqrt(sum(e)))
def _f_verdet(m, gamma, k): v = m.v * k.v / gamma.v e = [(m.e * k.v / gamma.v)**2, (gamma.e * m.v * k.v / gamma.v**2)**2, (k.e * m.v / gamma.v)**2] e = np.sqrt(sum(e)) v = v * 180 / np.pi * 60 e = e * 180 / np.pi * 60 return Value(v, e)
def get_gamma(self): model = self.analysis.model a = Value(model.params[0], model.param_errs[0]) b = Value(model.params[1], model.param_errs[1]) c = Value(model.params[2], model.param_errs[2]) print('a', a) print('b', b) print('c', c) alpha = self._f_alpha(self.L, self.delta_x) beta = self._f_beta(self.L, self.delta_x) print('alpha', alpha) print('beta', beta) gamma = self._f_gamma(a, b, c, alpha, beta) return gamma
def _f_gamma(a, b, c, alpha, beta): e = [(a.e / 3 * (beta.v**3 - alpha.v**3))**2, (b.e / 2 * (beta.v**2 - alpha.v**2))**2, (c.e * (beta.v - alpha.v))**2, (alpha.e * (a.v * alpha.v**2 + b.v * alpha.v + c.v))**2, (beta.e * (a.v * beta.v**2 + b.v * beta.v + c.v))**2] e = np.sqrt(sum(e)) def f(x): return a.v / 3 * x**3 + b.v / 2 * x**2 + c.v * x v = f(beta.v) - f(alpha.v) return Value(v, e)
def _get_I(raw_data): I1 = np.mean(raw_data[['I1', 'I3']], axis=1) I2 = np.mean(raw_data[['I2', 'I4']], axis=1) I1err = np.std(raw_data[['I1', 'I3']], ddof=1, axis=1) I2err = np.std(raw_data[['I2', 'I4']], ddof=1, axis=1) I1err = np.max([I1err, np.ones_like(I1)*0.01], axis=0) I2err = np.max([I2err, np.ones_like(I2)*0.01], axis=0) # I1err = np.sqrt(np.sum(raw_data[['I1 err', 'I3 err']], axis=1)) # I2err = np.sqrt(np.sum(raw_data[['I2 err', 'I4 err']], axis=1)) I = I2 - I1 Ierr = np.sqrt(I1err**2+I2err**2) return Value(I, Ierr)
def _get_m(raw_data): return Value(raw_data['mass'], 0.0001)
def get_slope(self): v = self.analysis.model.params[0] e = self.analysis.model.param_errs[0] return Value(v, e)
def _f_slope(m_water, m_air): v = m_water.v - m_air.v e = np.sqrt(m_water.e**2 + m_air.e**2) return Value(v, e)
def _f_alpha(L, delta_x): v = -L.v / 2 + delta_x.v e = np.sqrt((L.e**2 / 4 + delta_x.e**2)) return Value(v, e)
class Magnet: L = Value(9.4673, 0.0024, 'cm') delta_x = Value(0, .2, 'cm') def __init__(self, fname): x, y, yerr = self.get_data(fname) self.x = x self.y = y self.yerr = yerr self.analysis = self._analysis() def __str__(self): return 'Magnet:\n' \ '--------\n' \ 'Analysis\n' \ '--------\n' \ '{}\n\n' \ 'gamma: {}'.format(self.analysis, self.get_gamma()) def get_gamma(self): model = self.analysis.model a = Value(model.params[0], model.param_errs[0]) b = Value(model.params[1], model.param_errs[1]) c = Value(model.params[2], model.param_errs[2]) print('a', a) print('b', b) print('c', c) alpha = self._f_alpha(self.L, self.delta_x) beta = self._f_beta(self.L, self.delta_x) print('alpha', alpha) print('beta', beta) gamma = self._f_gamma(a, b, c, alpha, beta) return gamma @staticmethod def _f_beta(L, delta_x): v = L.v / 2 + delta_x.v e = np.sqrt((L.e**2 / 4 + delta_x.e**2)) return Value(v, e) @staticmethod def _f_alpha(L, delta_x): v = -L.v / 2 + delta_x.v e = np.sqrt((L.e**2 / 4 + delta_x.e**2)) return Value(v, e) @staticmethod def _f_gamma(a, b, c, alpha, beta): e = [(a.e / 3 * (beta.v**3 - alpha.v**3))**2, (b.e / 2 * (beta.v**2 - alpha.v**2))**2, (c.e * (beta.v - alpha.v))**2, (alpha.e * (a.v * alpha.v**2 + b.v * alpha.v + c.v))**2, (beta.e * (a.v * beta.v**2 + b.v * beta.v + c.v))**2] e = np.sqrt(sum(e)) def f(x): return a.v / 3 * x**3 + b.v / 2 * x**2 + c.v * x v = f(beta.v) - f(alpha.v) return Value(v, e) def plot(self, fig): fig.suptitle('Magnetic Field Calibration') ax = fig.add_subplot(121) self.analysis.plot(ax) ax = fig.add_subplot(122) self.analysis.plot_chi(ax) ax.grid() def _analysis(self): x, y, yerr = self.x, self.y, self.yerr def fit(x, a, b, c): return a * x**2 + b * x + c popt, pcov = scipy.optimize.curve_fit(fit, x, y, sigma=yerr) perr = np.sqrt(np.diag(pcov)) model = Model(fit, popt, perr) analysis = Analysis(model, x, y, yerr) return analysis @classmethod def get_data(cls, fname): data = pd.read_excel(fname, 'coil_calibration') x = data['x (cm)'] y = data['B (Gauss)'] yerr = np.ones_like(y) * 0.1 i = np.where(np.abs(x) < 5.25)[0] x = x[i] y = y[i] yerr = yerr[i] return x, y, yerr