Curve Fitting

This is the place for queries that don't fit in any of the other categories.

Curve Fitting

Postby cobi20 » Sat May 04, 2013 3:31 pm

Hey, I am currently writing a physics report- and as part of the requirements I must include a non-linear fitting of my data. I have my data in a .txt file, and I want to use this data to produce a gaussian fitting, there are two peaks I want to do the fit on over the data set, so I want to specify where the fitting takes place. I have got a general code from a gaussian fit I have done before. I have tried to just slightly edit it for the data I have this time. Here is what I have so far.:
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
Last edited by stranac on Sat May 04, 2013 7:39 pm, edited 1 time in total.
Reason: Added code tags
cobi20
 
Posts: 1
Joined: Sat May 04, 2013 3:25 pm

Re: Curve Fitting

Postby hansn » Sat May 04, 2013 6:02 pm

Please use code tags when pasting code. See this site for instructions: viewtopic.php?f=6&t=145
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Curve Fitting

Postby rrashkin » Sat May 04, 2013 8:10 pm

I think you ought to look at the scipy documentation, specifically the arguments to curve_fit. The error seems to be telling you that you specified N as 3 and M as 0 and that that's illegal. If I had to guess, I'd figure x1 and y1 as the culprits but I'd just be taking a wild swing. Then you need to decide how to correct it if that's what it is. Either start somewhere else in the data or trap the error and do something corrective (like "if N>M: N=M" or something).
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 4 guests