How to fit data with an arbitrary lorentzian function

121 ビュー (過去 30 日間)
Gaia Bublitz
Gaia Bublitz 2020 年 12 月 12 日
回答済み: Keerthana Chiruvolu 2020 年 12 月 16 日
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 Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by