How to fit data with an arbitrary lorentzian function

118 次查看(过去 30 天)
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.

回答(1 个)

Keerthana Chiruvolu
Keerthana Chiruvolu 2020-12-16
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.

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by