lab-II/optics/spectra.py
2018-03-18 18:02:21 +01:00

193 lines
4.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding: utf-8
from __future__ import division, print_function, unicode_literals
from lab import *
import uncertainties.umath as um
import uncertainties.unumpy as unp
import numpy as np
import matplotlib.pyplot as plt
# convert degrees to radians
angle = lambda x, y, z: (x + y/60 + z/3600) * np.pi/180
t0 = angle(180,0,0) # angle offset
te = angle(0,0,1) # angle uncertainty
##
## Part I
##
l = 5893e-10 # sodium doublet wavelength
n = array(1, 2) # line order
# line angles
t = array(angle(200,0,16) - angle(159,0,8),
angle(225,0,24) - angle(134,1,5))/2
# spacing (m⁻¹)
d = ufloat((n*l/np.sin(t)).mean(), 0.01e-6)
print(mformat('''
# Part I: diffraction grating spacing
## Na lamp
d: {} lines/mm
''', 1/d*1e-3))
##
## Part II
##
## lamp B
# line angles
t = uarray(te, angle(195,0,2 ), # violet
angle(195,0,15),
angle(195,1,1 ),
angle(195,1,20),
angle(195,1,30),
angle(199,1,9 ), # green
angle(200,1,21)) # orange
# wavelengths
l = d*unp.sin(t-t0)
print('''
# Part II
## lamp B - Xe?
λ:''', '\n '.join(mformat('{}', i) for i in l*1e9), 'nm')
## lamp C
# line angles
t = uarray(te, angle(193,1,11), # violet
angle(195,1,14),
angle(196,1,11), # green
angle(197,0,25),
angle(197,1,23),
angle(200,1,27),
angle(204,0,1 ))
# wavelengths
l = d*unp.sin(t-t0)
print('''
## lamp C - Kr?
λ:''', '\n '.join(mformat('{}', i) for i in l*1e9), 'nm')
## lamp D
# line angles
t = uarray(te, angle(194,1,12), # violet
angle(194,1,23),
angle(195,0,8 ),
angle(195,0,15),
angle(195,0,25),
angle(196,0,2 ), # blue
angle(196,0,10),
angle(196,1,13), # green
angle(198,0,15),
angle(198,0,22),
angle(198,1,5 ),
angle(199,1,2 ), # yellow
angle(199,1,12),
angle(200,0,0 ),
angle(200,0,11), # orange
angle(200,0,23),
angle(201,0,2 ),
angle(201,0,8 ),
angle(201,1,11)) # red
# wavelengths
l = d*unp.sin(t-t0)
print('''
## lamp D
λ:''', '\n '.join(mformat('{}', i) for i in l*1e9), 'nm')
##
## Part III
##
## Hg lamp
# prism angle
a = ufloat(angle(60,0,0), te)
# spectral line angles
t = array(
(angle(230,50,11), angle(230,44, 1), angle(230,1,21), angle(229,1,19), angle(228,16,4), angle(228,1,25), angle(228,1,19)),
(angle(230,50,25), angle(230,44,10), angle(230,1,28), angle(229,1,21), angle(228,16,4), angle(228,1,22), angle(228,1,21)),
(angle(230,50,11), angle(230,44, 5), angle(230,1,15), angle(229,1,12), angle(228,16,3), angle(228,1,13), angle(228,1,12)),
(angle(230,50,15), angle(230,44, 7), angle(230,1,20), angle(229,1,14), angle(228,16,2), angle(228,1,11), angle(228,1,10)))
# calculate mean values
t = uarray(1e-3, *map(np.mean, t.T))
# refractive index
n = unp.sin((a+t-t0)/2) / unp.sin(a/2)
# spectral lines wavelengths
l = array(4047e-10, # violet I
4077e-10, # violet II
4358e-10, # blue
4916e-10, # green I
5460e-10, # green II
5769e-10, # yellow I
5790e-10) # yellow II
# fit and plot n(λ)
f, (a,b) = curve(l, nominal(n), lambda x, a, b: a+b/x**2, sigma(n), guess=[1, 0.001])
alpha = chi_squared_fit(l, nominal(n), f, sigma(n))
x = np.linspace(400e-9, 580e-9, 100)
print(mformat('''
# Part III
## Hg lamp
a: {}
b: {}
χ² test: α={}
''', a, b, alpha))
plt.figure(1)
plt.xlabel('wavelength (nm)')
plt.ylabel('refractive index')
plt.scatter(l*1e9, nominal(n), color='#4f28fc')
plt.errorbar(l*1e9, nominal(n), yerr=sigma(n), linestyle='', color='#286bfc')
plt.plot(x*1e9, f(x), color='#5671c9')
plt.show()
## lamp B
t = array(angle(124,1,15), # violet
angle(125,0,0 ),
angle(125,0,5 ),
angle(125,0,16), # blue
angle(125,0,24),
angle(127,0,0 ), # green
angle(127,0,19), # yellow
angle(127,0,26), # orange
angle(127,1,12), # red
angle(128,0,25)) # red
## lamp C
t = array(angle(232,1,24), # red
angle(233,0,18),
angle(234,1,1 ), # yellow
angle(236,1,3 ), # green
angle(237,0,7 ),
angle(237,1,28), # blue
angle(239,0,10)) # violet