How to fit data with an arbitrary lorentzian function

110 views (last 30 days)
I created a function:
x0=1960
y=10
x=linspace(x0-100,x0+100,200)
F=(1/pi*y)*(y^2./(x-x0).^2+y^2)
plot(x,F,'LineWidth',1)
PlotLorentzian = [x', F']; % compile into data format
writematrix(PlotLorentzian,'plotlorentzian.m','filetype','text')
Now I want to fit data from OneLorentzian.txt to this function. OneLorentzian.txt has x in the first column and the output is F; the values of x0 and y are different than the values in the above function but the equation is the same. I need to write a code to fit this spectrum to the function I made, and determine the x0 and y values. The data has a Lorentzian curve shape.
data=load('OneLorentzian.txt')
fitx=data(:,1)
L=data(:,2)
plot(fitx,L)
Lorentzian=fittype(@(fitx,fity)(1/pi*fity)*(fity^2./(fitx-x02).^2+fity^2))
The last line of code isn't working and I'm stuck. Any help is appreciated.

Answers (1)

Keerthana Chiruvolu
Keerthana Chiruvolu on 16 Dec 2020
Hi,
The error in last line of code can be eliminated by,
  • Specifying the independent parameters in the function
  • Proper use dot operator( .^, .*)
  • Identifying any missing operators in x02. In case x02 is a variable, check if its defined in the code or MATLAB Workspace
Sample code:
Lorentzian=fittype(@(fitx,fity)(1/pi*fity).*(fity.^2./(fitx-x0*2).^2+fity.^2),'independent','fity')
Please refer the link for more information on creating fittypes with Anonymous functions.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!