def plot(x, y, file, fitfunktion, tau_erw, abklingzeit_erw, x_fit=None, xlim=None, ylim=None):
    plt.errorbar(unp.nominal_values(x), unp.nominal_values(y),
                 xerr=unp.std_devs(x), yerr=unp.std_devs(y), fmt='-', label='Messwerte')

    # maxima finden
    x_nom = noms(x)
    y_nom = noms(y)
    left_right = 1
    ende = 30
    start = 30
    maxima_x = []
    maxima_y = []
    for i in range(start, len(x_nom)-ende):
        if y_nom[i-left_right]<y_nom[i] and y_nom[i+left_right]<=y_nom[i] and y_nom[i-3]<y_nom[i] and y_nom[i+3]<y_nom[i]:
            maxima_x.append(x_nom[i])
            maxima_y.append(y_nom[i])
            #i += 500
    maxima_x, maxima_y = maxima_x[:14], maxima_y[:14]
    plt.plot(maxima_x, maxima_y, 'x', label='Maxima')
    maxima_x = np.asarray(maxima_x)
    U_0 = 0.000007
    # fitten:
    if x_fit is not None:
        params, covariance = curve_fit(fitfunktion, maxima_x,
                                        maxima_y,
                                        p0=[tau_erw.n, U_0])
        errors = np.sqrt(np.diag(covariance))
        print('tau= ', params[0] * 1e3, '±', errors[0] * 1e3)
        k = ufloat(params[0], errors[0])
        print('U_0= ', params[1], '±', errors[1])
        U_0 = ufloat(params[1], errors[1])

        print('Abweichung von tau_erw = ', abweichungen(tau_erw, k), '%')
        x_fit = np.linspace(x_fit[0], x_fit[1], 10000)
        plt.plot(x_fit, fitfunktion(x_fit, *params), label='Fit')
        label=r'Maximum$\:/\:e$'
        #label='test'
        plt.plot((xlim[0], xlim[1]), (maxima_y[0]/np.e, maxima_y[0]/np.e), label=label)

        for x_loop in x_fit:
            if fitfunktion(x_loop, *params) < maxima_y[0]/np.e:
                print('Abklingzeit =', x_loop - maxima_x[0], abklingzeit_erw, abweichungen(abklingzeit_erw, x_loop - maxima_x[0]))
                break

    if xlim is not None:
        plt.xlim(xlim[0], xlim[1])
    if ylim is not None:
        plt.ylim(ylim[0], ylim[1])
    xlabel = r'$t\:/\:\si{\second}$'
    ylabel = r"$U_\text{A}\:/\:\si{\volt}$"
    #xlabel, ylabel = 'test', 'test'
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.legend(loc='best')

    # in matplotlibrc leider (noch) nicht möglich
    plt.tight_layout(pad=0.2, h_pad=1.08, w_pad=1.08)
    plt.savefig('build/'+file+'.pdf')
    plt.close()
Beispiel #2
0
def plotElement(anodenstrom, spannung, V_N, R):
    spannung /= (V_N**2 * 1000**2 * 10)  # verstärkung rausrechnen
    x = anodenstrom*10**(-3)
    y = spannung/(R**2)  # I^2 bestimmt
    plt.errorbar(noms(x), noms(y),
    xerr=stds(x), yerr=stds(y), fmt='kx', label='Messwerte')

    # fitten:
    params, covariance = curve_fit(fitfunktion, unp.nominal_values(x),
                                   unp.nominal_values(y),
                                   p0=[1])
    errors = np.sqrt(np.diag(covariance))
    print('m= ', params[0], '±', errors[0])
    m = ufloat(params[0], errors[0])
    x_fit = np.linspace(-0.0001, max(noms(x))+0.001)
    plt.plot(x_fit, fitfunktion(x_fit, *params), label='Fit')

    plt.xlim(0, 0.0045)
    plt.ylim(0, 1.75e-17)
    plt.xlabel(r'$I_0 \:/\: \si{\ampere}$')
    plt.ylabel(r'$I^2 \:/\: \si{\ampere\squared}$')
    delta_nu = ufloat(24.4, 0.4)
    delta_nu *= 10**3
    e0 = m/(2*delta_nu)
    print("e0 = ", e0)
    e0theorie = ufloat(constants.physical_constants["elementary charge"][0],
                       constants.physical_constants["elementary charge"][2])
    print("Abweichung von Theorie= ", abweichungen(e0theorie, e0))
    plt.legend(loc='best')

    # in matplotlibrc leider (noch) nicht möglich
    plt.tight_layout(pad=0.2, h_pad=1.08, w_pad=1.08)
    plt.savefig('build/plotElement.pdf')
    plt.close()
def longitudinaleModen(f_array, L_vergleich):
    c = ufloat(constants.physical_constants["speed of light in vacuum"][0], 0)
    differenz = unp.uarray(np.zeros(4), np.zeros(4))
    for i in range(len(f_array)):
        if i == 0:
            differenz[i] = f_array[i]
        else:
            differenz[i] = f_array[i] - f_array[i-1]
    print(differenz)
    differenz *= 10**6
    L = c/((np.sum(differenz)/4)*2)
    print('L = ', L)
    print('Abweichung = ', abweichungen(L_vergleich, L))
def plotWiderstand(widerstand, spannung, V_N, dateiname, T):
    spannung = unp.uarray(spannung, 0.005)
    widerstand = unp.uarray(widerstand, 1)
    y = spannung / V_N**2
    x = widerstand
    plt.errorbar(unp.nominal_values(x),
                 unp.nominal_values(y),
                 xerr=unp.std_devs(x),
                 yerr=unp.std_devs(y),
                 fmt='kx',
                 label='Messwerte')

    # fitten:
    params, covariance = curve_fit(fitfunktion,
                                   unp.nominal_values(x),
                                   unp.nominal_values(y),
                                   p0=[0.1])
    errors = np.sqrt(np.diag(covariance))
    print('m= ', params[0], '±', errors[0])
    m = ufloat(params[0], errors[0])
    x_fit = np.linspace(-5, max(noms(x)) + 1000)
    plt.plot(x_fit, fitfunktion(x_fit, *params), label='Fit')

    # kBoltzmann
    if dateiname == "1" or dateiname == "2":
        #int, intf = np.genfromtxt('build/eichungeinfach.txt', unpack='True')
        int = ufloat(4.9486e11, 0.001e11)
    else:
        #int, intf = np.genfromtxt('build/eichungKorrelator.txt', unpack='True')
        int = ufloat(7.41e9, 0.04e9)

    #int = ufloat(int, intf)
    T = ufloat(296.15, 2)  # K
    k_B = m / (4 * int * T)
    print('k_B_', dateiname, " = ", k_B)
    kBTheorie = ufloat(constants.physical_constants["Boltzmann constant"][0],
                       constants.physical_constants["Boltzmann constant"][2])
    print("Abweichung von Theorie= ", abweichungen(kBTheorie, k_B))

    plt.xlabel(r'$R/\si{\ohm}$')
    plt.ylabel(r'$U_\text{A} \:/\: \si{\volt\squared}$')
    plt.xlim(0, 1.01 * max(noms(x)))
    plt.legend(loc='best')

    # in matplotlibrc leider (noch) nicht möglich
    #plt.tight_layout(pad=0, h_pad=1.08, w_pad=1.08)
    plt.savefig('build/plotWiderstand' + dateiname + '.pdf')
    plt.close()
Beispiel #5
0
def plot(axes, x, y, V_strich_theorie, label, filename, x_label, y_label, grenze, ylim=None, logy=None, uebergang=None, letzter_wert_kacke=False):
    #label = 'test'
    flanke_grenze = grenze
    flanke_grenze_oben = None
    if letzter_wert_kacke:
        flanke_grenze_oben = -1
        axes.errorbar(noms(x[-1:]), noms(y[-1:]),
                     xerr=stds(x[-1:]), yerr=stds(y[-1:]),
                     fmt='kx', label='Unberücksichtigt bei {}'.format(label))
    if uebergang is not None:
        flanke_grenze = grenze + 1
        axes.errorbar(noms(x[uebergang:uebergang+1]), noms(y[uebergang:uebergang+1]),
                     xerr=stds(x[uebergang:uebergang+1]), yerr=stds(y[uebergang:uebergang+1]),
                     fmt='gx', label='Messwerte Übergang bei {}'.format(label))

    axes.errorbar(noms(x[flanke_grenze:flanke_grenze_oben]), noms(y[flanke_grenze:flanke_grenze_oben]),
                 xerr=stds(x[flanke_grenze:flanke_grenze_oben]), yerr=stds(y[flanke_grenze:flanke_grenze_oben]),
                 fmt='rx', label='Messwerte Flanke bei {}'.format(label))
    axes.errorbar(noms(x[:grenze]), noms(y[:grenze]),
                 xerr=stds(x[:grenze]), yerr=stds(y[:grenze]),
                 fmt='bx', label='Messwerte Plateau bei {}'.format(label))
    plateau_mittel = ufloat(np.mean(np.exp(noms(y[:grenze]))), np.std(np.exp(noms(y[:grenze]))))
    plateau_mittel_halb = unp.log(plateau_mittel / unp.sqrt(2)) if plateau_mittel.n >= 1 else unp.log(plateau_mittel * unp.sqrt(2))
    label_v_halb = r"$V_\text{Plateau}' / \sqrt{2}$" if plateau_mittel.n >= 1 else r"$V_\text{Plateau}' \cdot \sqrt{2}$"
    #label_v_halb = 'test'
    axes.plot((min(noms(x)), max(noms(x))), (noms(plateau_mittel_halb), noms(plateau_mittel_halb)), '-', label=label_v_halb)
    grenzfrequenz = fitten(axes, x[flanke_grenze:flanke_grenze_oben], y[flanke_grenze:flanke_grenze_oben], linear, [-1, 5], ['m', 'b'], 'r', 'Flanke', schnittwert=plateau_mittel_halb)
    print('grenzfrequenz = ', grenzfrequenz)
    # fitten(x[:grenze], y[:grenze], linear, [0, 0], ['m', 'b'], 'g', 'Plateau')

    print('Mittelwert Plateau = ', plateau_mittel, 'Abweichung von ', abweichungen(V_strich_theorie, plateau_mittel))


    # plotting
    axes.legend(loc='best')
    axes.set_xlim(min(noms(x)) - max(noms(x)) * 0.06, (max(noms(x))) * 1.06)
    if ylim is not None:
        axes.set_ylim(ylim[0], ylim[1])
    #x_label, y_label = 'test', 'test'
    axes.set_xlabel(x_label)
    axes.set_ylabel(y_label)
    plt.close()

    return grenzfrequenz, plateau_mittel
def wellenlaenge(d_array, mitte, L):
    d_links = unp.uarray(np.zeros(mitte), np.zeros(mitte))
    d_rechts = unp.uarray(np.zeros(12-mitte), np.zeros(12-mitte))
    for i in range(0, mitte, 1):
        d_links[i] = np.sum(d_array[mitte-(i+1):mitte])
    for i in range(0, 12-mitte, 1):
        d_rechts[i] = np.sum(d_array[mitte:mitte+(i+1)])
    g = (10**-3)/80
    print('g = ', g*10**6, 'micro meter')
    d_links = d_links*10**-2
    d_rechts = d_rechts*10**-2
    d_all = np.concatenate((noms(d_links), noms(d_rechts)))
    n_links = np.linspace(1, 6, 6)
    lamda_links = g * unp.sin(unp.arctan(d_links/L))/n_links
    n_rechts = range(1, len(d_rechts)+1, 1)
    n_all = np.concatenate((n_links, n_rechts))
    lamda_rechts = g * unp.sin(unp.arctan(d_rechts/L))/n_rechts
    lamda_all = np.concatenate((lamda_links, lamda_rechts))
    werteZuTabelle(d_all*100, n_all.astype(int), (noms(lamda_all)*10**9).astype(int), (stds(lamda_all)*10**9).astype(int), rundungen=[3, 0, 0, 0])
    print('lambda = ', (np.sum(lamda_all))/12)
    print('Abweichung = ', abweichungen(632.8*10**-9, (np.sum(lamda_all))/12))
    print('Mittelwert = ', np.mean(noms(lamda_all)), '±', np.std(noms(lamda_all)))
import numpy as np
from uncertainties import ufloat
from functions import abweichungen

if __name__ == '__main__':
    # schalter
    R_1 = ufloat(1.00e3, 0.05e3)  #kohm
    R_p = ufloat(99.7e3, 0.5e3)  #kohm
    U_B = ufloat(14.13, 0.05)  #volt
    U_A = ufloat(26.3, 0.5)  #volt

    umkipp_1 = ufloat(151.25e-3, 5e-3)  #mV
    umkipp_2 = ufloat(167.75e-3, 5e-3)  #mV
    print(umkipp_1, U_B * R_1 / R_p, 'Abweichung = ',
          abweichungen(U_B * R_1 / R_p, umkipp_1))
    print(umkipp_2, U_B * R_1 / R_p, 'Abweichung = ',
          abweichungen(U_B * R_1 / R_p, umkipp_2))
    print('U_A', U_A, abweichungen(2 * U_B, U_A))

    #signalgen
    R_1 = ufloat(1.002e3, 0.05e3)  #kohm
    R_p = ufloat(99.7e3, 0.5e3)  #kohm
    R = ufloat(100e3, 0.5e3)  #kohm
    C = ufloat(970e-9, 10e-9)
    U_B = ufloat(14.125, 0.05)  #volt
    U_A_recht = ufloat(26.3, 0.5)  #volt
    U_A_drei = ufloat(345e-3, 5e-3)
    nu = ufloat(216, 1)
    U_A_drei_theo = 2 * R_1 / R_p * U_B
    U_A_recht_theo = 2 * U_B
    nu_drei_theo = (R_p) / (4 * R * C * R_1)
def plotSpektrum(nu_M, strom, V_N, delta_nu, dateiname):

    strom /= (V_N**2 * 1000**2 * 10)
    x = nu_M
    y = strom / delta_nu
    if dateiname == 'Oxid':
        how_many = 10
        x_funkel = x[len(x) - how_many:len(x)]
        x_schrot = x[0:len(x) - how_many]
        y_funkel = y[len(x) - how_many:len(x)]
        y_schrot = y[0:len(x) - how_many]
        print('nu_funkel <= ', x_funkel[0])
        plt.errorbar(noms(x_schrot),
                     noms(y_schrot),
                     xerr=stds(x_schrot),
                     yerr=stds(y_schrot),
                     fmt='kx',
                     label='Messwerte Schrotrauschen')
        plt.errorbar(noms(x_funkel),
                     noms(y_funkel),
                     xerr=stds(x_funkel),
                     yerr=stds(y_funkel),
                     fmt='rx',
                     label='Messwerte Schrotrauschen+Funkeleffekt')
        # fitten:
        params, covariance = curve_fit(funkelFunk,
                                       unp.nominal_values(x_funkel),
                                       unp.nominal_values(y_funkel),
                                       p0=[10**(-22), 1],
                                       sigma=stds(y_funkel))
        errors = np.sqrt(np.diag(covariance))
        print('const= ', params[0], '±', errors[0], ' alpha= ', params[1], '±',
              errors[1])
        alpha = ufloat(params[1], errors[1])
        print('Abweichung von alpha= ', abweichungen(1, alpha), '%')
        x_fit = np.linspace(0.02, 100)
        plt.plot(x_fit, funkelFunk(x_fit, *params), label='Fit')
        plt.xlim(0.03, 500)
        plt.ylim(10**(-22), 10**(-19))
    else:
        plt.errorbar(noms(x),
                     noms(y),
                     xerr=stds(x),
                     yerr=stds(y),
                     fmt='kx',
                     label='Messwerte')
    e0theorie = ufloat(constants.physical_constants["elementary charge"][0],
                       constants.physical_constants["elementary charge"][2])
    # xv = [0.02, 10**3]
    # yv = [2*e0theorie.n*0.9*10**(-3), 2*e0theorie.n*0.9*10**(-3)]
    # plt.plot(xv, yv, 'b-', label='Schrotrauschen Theorie')
    plt.xlabel(r'$\nu_\text{M}\:/\: \si{\kilo\hertz}$')
    plt.ylabel(r'$W \:/\: \si{\ampere\per\hertz}$')
    plt.legend(loc='best')
    plt.yscale('log')
    # plt.ylim(10**(-9), 10**(-6))
    plt.xscale('log')

    # in matplotlibrc leider (noch) nicht möglich
    plt.tight_layout(pad=0.5, h_pad=1.2, w_pad=1.08)
    plt.savefig('build/plotRauschspektrum' + dateiname + '.pdf')
    plt.close()
def plot(x,
         y,
         file,
         R,
         C,
         fitfunktion,
         k_guess,
         x_fit,
         xlim=None,
         ylim=None,
         int=False):

    if int:
        x = unp.log(x)
        y = unp.log(y)
    plt.errorbar(unp.nominal_values(x),
                 unp.nominal_values(y),
                 xerr=unp.std_devs(x),
                 yerr=unp.std_devs(y),
                 fmt='x',
                 label='Messwerte')

    # fitten:
    if int:
        params, covariance = curve_fit(fitfunktion,
                                       unp.nominal_values(x),
                                       unp.nominal_values(y),
                                       p0=[-1, -1 * np.log(R.n * C.n)])
        errors = np.sqrt(np.diag(covariance))
        print('m= ', params[0], '±', errors[0])
        print('b= ', params[1], '±', errors[1])
        m = ufloat(params[0], errors[0])
        b = ufloat(params[1], errors[1])
        rc_bestimmt = unp.exp(b / m)
        print('k = ', rc_bestimmt)
        print('Abweichung von R*C = ', abweichungen(R * C, rc_bestimmt), '%')
        x_fit = np.linspace(x_fit[0], x_fit[1])
    else:
        params, covariance = curve_fit(fitfunktion,
                                       unp.nominal_values(x),
                                       unp.nominal_values(y),
                                       p0=[k_guess])
        errors = np.sqrt(np.diag(covariance))
        print('m= ', params[0], '±', errors[0])
        k = ufloat(params[0], errors[0])

        print('Abweichung von R*C = ', abweichungen(R * C, k), '%')
        x_fit = np.linspace(x_fit[0], x_fit[1])
    plt.plot(x_fit, fitfunktion(x_fit, *params), label='Fit')

    if xlim is not None:
        plt.xlim(xlim[0], xlim[1])
    if ylim is not None:
        plt.ylim(ylim[0], ylim[1])
    if int:
        xlabel = r'$\ln(\omega\:/\:\si{\hertz})$'
        ylabel = r"$\ln(V')$"
    else:
        xlabel = r'$\omega\:/\:\si{\hertz}$'
        ylabel = r"$V'$"
    #xlabel, ylabel = 'test', 'test'
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.legend(loc='best')

    # in matplotlibrc leider (noch) nicht möglich
    plt.tight_layout(pad=0.2, h_pad=1.08, w_pad=1.08)
    plt.savefig('build/' + file + '.pdf')
    plt.close()