- Code: Select all
`from scipy.optimize import curve_fit`

from numpy 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[12:13.5]

y1=y[235:973]

p_guess = array([1, 12, 13.5])# 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

However I keep getting the error of :

- Code: Select all
`Traceback (most recent call last):`

File "C:\Users\Owner\Documents\Python\non-linear fitting.py", line 24, in <module>

popt, pcov = curve_fit(Gau, x1, y1, p_guess) # do fit

File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 434, in curve_fit

res = leastsq(func, p0, args=args, full_output=1, **kw)

File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 278, in leastsq

raise TypeError('Improper input: N=%s must not exceed M=%s' % (n,m))

TypeError: Improper input: N=3 must not exceed M=0

I don't know what to do, and how to change it. Any help would be welcome!!!

Thanks