lab-II/circuits/C3-?.py
2018-03-18 18:02:21 +01:00

189 lines
4.5 KiB
Python
Raw 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 print_function, division, unicode_literals
import numpy as np
import uncertainties.umath as um
import matplotlib.pyplot as plt
from lab import *
## Impedence of an inductor (I)
## (all SI units)
R = ufloat(996, 4) # resistor
L = ufloat(0.014561, 0.000009)
C = ufloat(10e-12, 1e-12)
# frequency
nu = array( 2e3, 10e3, 25e3, 50e3, 75e3, 100e3, 125e3, 150e3, 175e3, 200e3,
225e3, 250e3, 260e3, 270e3, 275e3, 285e3, 300e3, 350e3, 375e3)
# V input
Va = array(10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0,
10.0, 10.0, 8.2, 10.0, 10.0, 8.20, 10.0, 10.0, 10.0)/2
# V output
Vb = array( 10.0, 9.4, 7.8, 5.0, 3.28, 2.20, 1.44, 0.96, 0.56, 0.32, 0.144,
0.085, 0.0396, 0.069, 75.4e-3, 0.144, 0.194, 0.320, 0.360)/2
# time offset Va - Vb
Oab = array( 12e-6, 6.0e-6, 5.2e-6, 4.2e-6, 3.5e-6, 3.08e-6, 2.64e-6, 2.36e-6,
2.08e-6, 1.88e-6, 1.71e-6, 260e-9, 260e-9, 280e-9, 280e-9, 300e-9,
250e-9, 220e-9, 180e-9)
# time offset I - V
Oiv = array( 110e-6, 26.0e-6, 11.2e-6, 5.9e-6, 4.2e-6, 3.36e-6, 2.80e-6, 2.40e-6,
2.12e-6, 1.90e-6, 1.74e-6, 280e-9, 260e-9, 270e-9, 280e-9, 280e-9,
260e-9, 220e-9, 184e-9)
om = 2*np.pi*nu # angular frequency
I = Vb/R.n # output current
Vab = Va - Vb # tension drop
Fab = om * Oab # phase difference Vᵢ - Vₒ
Fiv = om * Oiv # phase difference I - Vₒ
Z = Vab/I * np.exp(1j*Fiv) # impedance
H1 = Vab/Vb * np.exp(1j*Fab) # transfer function ΔV→Vb
H2 = Vb/Va * np.exp(1j*Fab) # transfer function Va→Vb
# estimate uncertainties
Evb, Eva = ufloat(Vb[0], 1e-3), ufloat(Va[0], 0.1)
sigmaF = (om[0]*ufloat(Oiv[0], 3e-5)).s
sigmaZ = (R*(Eva - Evb)/Evb).s
sigmaH1 = ((Eva - Evb)/Evb).s
sigmaH2 = ((Evb - Eva)/Evb).s
# plot and fit Z(ν)
plt.figure(4)
plt.clf()
# magnitude
plt.subplot(2, 1, 1)
plt.title('impedance (RL circuit)')
plt.ylabel('magnitude (kΩ)')
plt.semilogx(nu, abs(Z)/1e3, 'o', color="#36913d", markersize=4.5)
# fit Y=kX where Y=|Z|, X=ν, k=2πL
k = simple_linear(nu, abs(Z), sigmaZ)
L = k/(2*np.pi)
f = lambda x: k.n*x
x = np.arange(nu.min()-10, nu.max(), 10)
plt.semilogx(x, f(x)/1e3, color='#589f22')
# phase
plt.subplot(2, 1, 2)
plt.xlabel('frequency (Hz)')
plt.ylabel('phase (rad)')
plt.semilogx(nu, Fiv, 'o', color="#36913d", markersize=4.5)
plt.show()
alpha = chi_squared_fit(nu, abs(Z), f, sigmaZ)
print(mformat('''
k: {}
L: {} H
χ² test:
α={:.2f}, α>ε: {}
''', k, L,
alpha, alpha>epsilon))
# plot, fit H₁(ν)
plt.figure(5)
plt.clf()
# magnitude
plt.subplot(2,1,1)
plt.title('transfer function 1')
plt.ylabel('magnitude (Vout-Vin / Vout)')
plt.semilogx(nu, abs(H1), 'o', color="#9b2e83", markersize=4.5)
# fit Y=kX where Y=|H1|, X=ν, k=2πL/R
k = simple_linear(nu, abs(H1), sigmaH1)
L = k*R/(2*np.pi)
f = lambda x: k.n/x
x = np.arange(nu.min()-10, nu.max(), 10)
plt.semilogx(x, f(x), color='#9b2e83')
# phase
plt.subplot(2,1,2)
plt.xlabel('frequency (Hz)')
plt.ylabel('phase (rad)')
plt.semilogx(nu, Fab, 'o', color="#3a44ad", markersize=4.5)
plt.show()
alpha = check_measures(R*C, RCo)
beta = chi_squared_fit(nu, abs(H1), f, sigmaH1)
print(mformat('''
k: {} Hz
RC: {} s
RCₒ: {} s
compatibility test:
α={:.2f}, α>ε: {}
χ² test:
β={:.2f}, β>ε: {}
''', k, R*C, RCo,
alpha, alpha>epsilon,
beta, beta>epsilon))
# plot, fit H₂(ν)
plt.figure(6)
plt.clf()
# magnitude
plt.subplot(2,1,1)
plt.title('transfer function 2')
plt.ylabel('magnitude (Vout / Vin)')
plt.semilogx(nu, abs(H2), 'o', color="#9b2e83", markersize=4.5)
# fit Y=a+bX where Y=1/|H₂|², X=1/ν², a=1, b=1/(2πRC)²
a,b = linear(1/nu**2, 1/abs(H2)**2, 0.01)
RCo = 1/(2*np.pi*um.sqrt(b))
f = lambda x: 1/np.sqrt(1 + b.n/x**2) # magnitude
g = lambda x: -np.pi/2 + np.arctan(2*np.pi*x*R.n*C.n) # phase
x = np.arange(nu.min()-10, nu.max(), 10)
plt.semilogx(x, f(x), color='#9b2e83')
# phase
plt.subplot(2,1,2)
plt.xlabel('frequency (Hz)')
plt.ylabel('phase (rad)')
plt.semilogx(nu, Fab, 'o', color="#3a44ad", markersize=4.5)
plt.semilogx(x, g(x))
plt.show()
alpha = check_measures(R*C, RCo)
beta = chi_squared_fit(nu, abs(H2), f, sigmaH2)
gamma = chi_squared_fit(nu, Fab, g, sigmaF)
print(mformat('''
b: {}
RC: {} s
RCₒ: {} s
compatibility test:
α={:.2f}, α>ε: {}
χ² test (magnitude):
β={:.2f}, β>ε: {}
χ² test (phase):
γ={:.2f}, γ>ε: {}
''', b, R*C, RCo,
alpha, alpha>epsilon,
beta, beta>epsilon,
gamma, gamma>epsilon))