Lab-I/corda.py
2016-06-19 20:37:57 +02:00

146 lines
3.3 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, unicode_literals
from lab import *
import numpy as np
import matplotlib.pyplot as plt
import uncertainties.umath as u
mass = lambda x: ufloat(x, 0.01)*0.001
length = lambda x: ufloat(x, 0.5)*0.01
##
## Costants
##
mL = [ (16.47, 251.0) # violet
, (5.49, 252.7) # black
, (6.16, 287.7) # green
, (1.48, 188.8) # white
]
mu = [mass(m)/length(l) for m,l in mL] # linear density (kg/m)
pp = mass(50.03) # tare mass (kg)
g = 9.80665 # acceleration due to gravity (m/s^2)
sigma = 0.8 # frequency uncertainty
## green string
## T,L,μ constant, plot ν(n)
m = mass(400.21) + pp
L = length(98.5)
T = m*g
n = np.arange(1,8)
nu = np.array([23.97, 48.20, 72.54, 96.97, 122.20, 147.10, 171.30])
# linear regression y=kx where
# y=ν, x=n, k=1/(2L)√(T/μ)
x = np.linspace(0,8,100)
k = simple_linear(n, nu, sigma)
f = lambda x: k.n*x
plt.figure(1)
plt.xlim(0,8)
plt.ylim(0,200)
plt.xlabel('n armonica')
plt.ylabel('frequenza di risonanza (Hz)')
plt.scatter(n, nu, color='#468174')
plt.plot(x, f(x), '#5ca897')
plt.show()
# χ² test
alpha = chi_squared_fit(n, nu, f, sigma, s=1)
v0 = 2*L*k # actual propagation velocity
v = u.sqrt(T/mu[2]) # theoretical propagation velocity
print '''
χ²: α={:.3f}, α>ε: {}
k: ({})1/s
v°: ({})m/s
v: ({})m/s
'''.format(alpha, alpha>epsilon, k, v0, v)
##
## green string
## T,μ,n constant, plot ν(T), n=2
M = [mass(i)+pp for i in [700.45, 499.95, 450.16, 400.19, 200.16, 140.92]]
T = np.array([g*i.n for i in M])
nu = np.array([60.26, 52.20, 49.70, 47.06, 34.96, 31.22])
# linear regression y=kx where
# y=ν, x=√T, k=1/(L√μ)
x = np.linspace(1, 8, 100)
k = simple_linear(np.sqrt(T), nu, sigma)
f = lambda x: k.n*np.sqrt(x)
plt.figure(2)
plt.xlabel('Tensione (N)')
plt.ylabel('Frequenza 2ª armonica (Hz)')
plt.scatter(T, nu, color='#673f73')
plt.plot(x, f(x), '#975ca8')
plt.show()
# χ² test
alpha = chi_squared_fit(T, nu, f, sigma, s=1)
print "χ²: α={}, α>ε: {}".format(alpha, alpha>epsilon)
##
## green string
## T,μ,n constant, plot ν(L), n=2
m = mass(400.21) + pp
T = m*g
L = np.array([116.5, 104.5, 95.4, 82.2, 75.0, 64.5, 54.7])*0.01
nu = np.array([44.58, 49.43, 54.60, 63.58, 69.34, 80.75, 96.40])
# linear regression y=kx where
# y=ν, x=1/L, k=n/2 √(T/μ)
x = np.linspace(0.5, 1.25, 100)
k = simple_linear(1/L, nu, sigma)
f = lambda x: k.n/x
plt.figure(3)
plt.xlabel('Lunghezza corda (m)')
plt.ylabel('Frequenza 2ª armonica (Hz)')
plt.scatter(L, nu, color='#844855')
plt.plot(x, f(x), '#a85c6c')
plt.show()
# χ² test
alpha = chi_squared_fit(L, nu, f, sigma, s=1)
print "χ^2: α={}, α>ε: {}".format(alpha, alpha>epsilon)
##
## every string
## T,n,L constant, plot ν(μ), n=2
m = mass(400.21) + pp
L = length(98.5)
n = 2
mu = np.array([i.n for i in mu])
nu = np.array([26.90, 47.23, 47.60, 78.20])
# linear regression y=kx where
# y=ν, x=1/√μ, k=√T/L
x = np.linspace(0.0005,0.007,100)
k = simple_linear(1/np.sqrt(mu), nu, sigma)
f = lambda x: k.n/np.sqrt(x)
plt.figure(4)
plt.xlabel('Densità lineare (kg/m)')
plt.ylabel('Frequenza 2ª armonica')
plt.scatter(mu, nu, color='#44507c')
plt.plot(x, f(x), '#5c6ca8')
plt.show()
# χ² test
alpha = chi_squared_fit(mu, nu, f, sigma, s=1)
print "χ²: α={}, α>ε: {}".format(alpha, alpha>epsilon)