How to fit data with an arbitrary lorentzian function

조회 수: 111 (최근 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 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