Firstly this is an assignment I've been set so I'm only after pointers, and I am restricted to using the following libraries, NumPy, SciPy and MatPlotLib.
We have been given a txt file which includes x and y data for a resonance experiment and have to fit both a gaussian and lorentzian fit. I'm working on the gaussian fit at the minute and have tried following the code laid out in a previous question as a basis for my own code. (Gaussian fit for Python)
from numpy import *
from matplotlib import *
import matplotlib.pyplot as plt ##Import Libraries
import pylab
from scipy.optimize import curve_fit
####################################
energy,intensity=numpy.loadtxt('resonance_data.txt',unpack=True)
print energy
print intensity ##Load in text file and print the arrays
#####################################
n = size(energy)
mean = 30.7
sigma = 10
intensity0 = 45
def gaus(energy,intensity0,energy0,sigma):
return intensity0*exp(-(energy-energy0)**2/(sigma**2))
popt,pcov = curve_fit(gaus,energy,intensity,p0=[45,mean,sigma])
plt.plot(energy,intensity,'o')
plt.xlabel('Energy/eV')
plt.ylabel('Intensity')
plt.title('Plot of Intensity against Energy') ##Plot raw data along with axis labels and title
plt.plot(energy,gaus(energy,*popt))
plt.show()
Which returns the following graph
If I keep the expressions for mean and sigma, as in the url posted the curve fit is a horizontal line, so I'm guessing the problem lies in the curve fit not converging or something.
I am somewhat of a beginner so any pointers are helpful :)
Thanks
UPDATE: I've managed to improve the Gaussian fit and get the Lorentzian working, and even (I think) managed to calculate the sum of the residuals in each case.
Cheers guys!
Thanks again guys