ich würde gerne für einen Praktikumsversuch aus meinem Studium eine Überlagerung von zwei Gaußkurven an meine Messwerte fitten. Das hab ich auch soweit schon geschafft, ich würde aber beim Fit gerne die Messfehler mit berücksichtigen, weiß aber nicht wie das geht. Hat einer von euch vielleicht Ahnung von sowas?
Hier ist mein Code:
Code: Alles auswählen
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
data = np.loadtxt("langzeit.txt", delimiter="\t", dtype=float, unpack=True)
######################### Konfiguration der Grafik #############################
plt.axis([29.8, 30.8, 2, 16])
plt.figure(figsize = (10, 6), dpi = 500)
plt.grid(True)
plt.tight_layout()
plt.xlabel('$\u03b8 / °$')
plt.ylabel('$Zählate / s^{-1}$')
########################## Definition der Fitfunktion ##########################
def gauss(x,mu,sigma,A,offset):
return A*np.exp(-(x-mu)**2/2/sigma**2)+offset
def doppelgauss(x, mu1, sigma1, A1, offset1, mu2, sigma2, A2, offset2):
return gauss(x, mu1, sigma1, A1, offset1) + gauss(x, mu2, sigma2, A2, offset2)
####################### Anwendung auf die Messwerte ############################
expected = [30.19, 0.3, 15.8, 0, 30.37, 0.2, 1, 0]
params_1, cov = curve_fit(doppelgauss, data[0][147:210], data[1][147:210], expected)
print("mu1 = %f +- %f" % (params_1[0], np.diag(cov)[0]))
print("mu2 = %f +- %f" % (params_1[4], np.diag(cov)[4]))
########################## Erzeugung der Grafik ################################
x_fit1 = np.linspace(29.96, 30.59, 10000)
plt.plot(x_fit1, doppelgauss(x_fit1, *params_1), c = 'r', linewidth = 1) #Gaußfit
plt.errorbar(data[0], data[1], xerr = data[2], yerr = data[3], linestyle = 'none', capsize = 1, markersize = 0.5, elinewidth = 0.5, ecolor = 'black') #Messwerte
plt.savefig("langzeit.png", orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None, metadata=None)
