Skip to content

Instantly share code, notes, and snippets.

Created May 6, 2013 23:44
Show Gist options
  • Save anonymous/5529209 to your computer and use it in GitHub Desktop.
Save anonymous/5529209 to your computer and use it in GitHub Desktop.
from scipy.optimize import curve_fit
from numpy import *
from matplotlib.pyplot import *
x,y=loadtxt("7.675cm.txt",unpack=True)
def Gau(x1, height, mu, sigma):
return height*exp(-1/2*((x1-mu)/sigma)**2)
#First peak
x1=x[24:39]
y1=y[24:39]
p_guess = array([1, 1, 1])# initial guesses for peak
popt, pcov = curve_fit(Gau, x1, y1, p_guess) # do fit
plot(x1, Gau(x1, *popt), 'r-') # plot fit
plot(x,y, 'b.')
print'First Peak'
print 'Height: ', popt[0], '+/-', sqrt(pcov.diagonal()[0]) # the fitted values
print 'Mu: ', popt[1], '+/-', sqrt(pcov.diagonal()[1]) # the fitted values
print 'Sigma: ', popt[2], '+/-', sqrt(pcov.diagonal()[2]) # the fitted values
#Second Peak
x2=x[14.5:15.5]
y2=y[14.5:15.5]
p_guess = array([1, 550, 50]) # initial guess for peak
popt, pcov = curve_fit(Gau, x2, y2, p_guess) # do fit
plot(x2, Gau(x2, *popt), 'g-') # plot fit
print'Second Peak'
print 'Height: ', popt[0], '+/-', sqrt(pcov.diagonal()[0]) # the fitted values
print 'Mu: ', popt[1], '+/-', sqrt(pcov.diagonal()[1]) # the fitted values
print 'Sigma: ', popt[2], '+/-', sqrt(pcov.diagonal()[2]) # the fitted values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment