File Exchange

image thumbnail


version (6.84 KB) by Jered Wells
LORENTZFIT fits a single- or multi-parameter Lorentzian function to data


Updated 15 Jun 2020

View Version History

View License

% LORENTZFIT fits a single- or multi-parameter Lorentzian function to data
% LORENTZFIT(X,Y) returns YPRIME(X), a Lorentzian fit to the data
% found using LSQCURVEFIT. The function Y(X) is fit by the model:
% YPRIME(X) = P1./((X - P2).^2 + P3) + C.
% values in addition to fit-parameters PARAMS = [P1 P2 P3 C]. The RESNORM,
% RESIDUAL, and JACOBIAN outputs from LSQCURVEFIT are also returned.
% [...] = LORENTZFIT(X,Y,P0) can be used to provide starting
% values (P0 = [P01 P02 P03 C0]) for the parameters in PARAMS.
% [...] = LORENTZFIT(X,Y,P0,BOUNDS) may be used to define lower
% and upper bounds for the possbile values for each parameter in PARAMS.
% BOUNDS = [LB1 LB2 LB3 LB4;
% UB1 UB2 UB3 UB4].
% If the user does not wish to manually define values for P0, it may be
% enetered as an empty matrix P0 = []. In this case, default values will
% be used. The default bounds for all parameters are (-Inf,Inf).
% [...] = LORENTZFIT(X,Y,P0,BOUNDS,NPARAMS) may be used to specify the
% number of parameters used in the Lorentzian fitting function. The
% number of parameters defined in P0 and BOUNDS must match the function
% specified by NPARAMS. If the user does not wish to manually define
% values for P0 or BOUNDS, both may be enetered as empty matricies:
% P0 = []; BOUNDS = [].
% -NPARAMS options
% '1' - Single parameter Lorentzian (no constant term)
% L1(X) = 1./(P1(X.^2 + 1))
% '1c' - Single parameter Lorentzian (with constant term)
% L1C(X) = 1./(P1(X.^2 + 1)) + C
% '2' - Two parameter Lorentzian (no constant term)
% L2(X) = P1./(X.^2 + P2)
% '2c' - Two parameter Lorentzian (with constant term)
% L2C(X) = P1./(X.^2 + P2) + C
% '3' - Three parameter Lorentzian (no constant term)
% L3(X) = P1./((X - P2).^2 + P3)
% [DEFAULT] '3c' - Three parameter Lorentzian (with constant term)
% L3C(X) = P1./((X - P2).^2 + P3) + C
% array for the MATLAB function LSQCURVEFIT. OPTIONS can be set using the
% following command:
% OPTIONS = optimset('PARAM1',VALUE1,'PARAM2',VALUE2,...);
% See the help documentation for OPTIMSET for more details.
% X and Y must be the same size, numeric, and non-complex. P0 and BOUNDS
% must also be numeric and non-complex. NPARAMS is a character array.
% Examples:
% x = -16:0.1:35;
% y = 19.4./((x - 7).^2 + 15.8) + randn(size(x))./10;
% [yprime1 params1 resnorm1 residual1 jacobain1] = lorentzfit(x,y,[20 10 15 0]);
% figure; plot(x,y,'b.','LineWidth',2)
% hold on; plot(x,yprime1,'r-','LineWidth',2)
% [yprime2 params2 resnorm2 residual2 jacobian2] = lorentzfit(x,y,[],[],'3');
% figure; plot(x,y,'b.','LineWidth',2)
% hold on; plot(x,yprime2,'r-','LineWidth',2)
% See also: lsqcurvefit.

Cite As

Jered Wells (2021). lorentzfit(x,y,varargin) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (41)

yaoxing wu

Hello! Thank you for the code.
This function sometimes cannot fit well for my data. I use the three parameter Lorentzian fitting (3c) to fit them but the fitting results are very bad. Could you please help me to check why it returns a bad fitting?
Below is my data:
x = [346.01 346.023 346.036 346.049 346.062 346.075 346.088 346.101 346.114 346.127 346.14 346.152 346.165 346.178 346.191 346.204 346.217 346.23 346.243 346.256 346.269 346.282 346.294 346.307 346.32 346.333 346.346 346.359 346.372 346.385 346.398];
y = [35014 35801 35586 37569 41358 43500 47502 52447 63223 88424 129550 168448 196061 190554 154673 113933 82236 62404 50488 43820 37823 32385 30089 28465 27012 27126 26678 27031 28048 24680 24937];

Thank you.
Yaoxing Wu

yaoxing wu

Is there anything wrong about this file? A message showed that the file is broken after I downloaded it. Thank you.

Tom Lenkiewicz

Yusuf Karli

thank you for the code.
is there any way to create cfit file after fit? i can get parameters as an array but i am asking for cfit file.
thank you again.

xin li

Thank you. You are a good man. God bless you.

Jered Wells

@J Shaw, you should try modifying the P0 input values so that they are closer to what you expect for your specific dataset. A local minimum message suggests that the fit is not fully optimized and your starting (P0) values are too far off - the fit is getting hung in a local optimum. You might also define some parameter boundaries in the input BOUNDS to constrain the fit and guide it to the global minimum of the optimization function.

J Shaw

Hi Jared! I'm trying to retrieve my the fit parameters, but keep getting "Local minimum found.

Optimization completed because the size of the gradient is less than
the selected value of the optimality tolerance.

<stopping criteria details>"

Any ideas?

haibin cui

Su-Yeon Kim

Hello Jared. I am working on LIBS spectrum. I want to fit the pulses with lorenzian fitting. I confused about the input given to this function. Please help.

Jered Wells

Eric, without seeing your data, it is hard to understand what you need. If the dominant function driving the data is indeed Lorentzian, then no modification of this code should be necessary. Otherwise, you may need to adjust the fitting function to suit your specific needs. Email me if you have further questions.

Eric Patterson

Hi Jered, how can I use this function with the data I have. I want the Lorentz curve to fit under multiple peaks of a certain peak value. Sorry I am new to MATLAB.

Yaroslav Nikolaev

Cool, thank you for the link Jered!

Jered Wells

Yaroslav, here is a link to MATLAB's recommendation for citing File Exchange files. Thank you for your citation!

Yaroslav Nikolaev

Hi Jered, thank you for this function - very useful! :-) Can I cite your work on this somehow in a scientific publication?


Wei Weng

ruibin liu

Sara Ameli

Hi Jered, thanks for the code.
In the 3 parameter case, is it possible to have P3=(pi*P1)^2? to relate them to the same \gama in Lorentzian distribution?

Leo YU

Hi Jared, I met some problems using this function. Could you please check this? Thank you.
' Local minimum possible.

lsqcurvefit stopped because the size of the current step is less than
the selected value of the step size tolerance.

<stopping criteria details>'

'Local minimum found.

Optimization completed because the size of the gradient is less than
the selected value of the optimality tolerance.

<stopping criteria details>

Optimization completed: The first-order optimality measure, 7.891109e-11,
is less than options.OptimalityTolerance = 1.366818e-09.

Optimization Metric Options
relative first-order optimality = 7.89e-11 OptimalityTolerance = 1e-09 (selected)'

which settings I should change? Cheers.

Jered Wells

Joseph. To get the covariance matrix associated with the fit variables, you can follow the instructions here: Using the LORENTZFIT code, you will need to output the Jacobian from the call to LSQCURVEFIT. This will require a little editing on your part, but should be fairly straightforward. Hope this helps.

Joseph Horne

Jared, I'm using a Lorentz fit to find the position of a single peak to quote it as a resonance value. If it is possible to find the uncertainty in the value, or some uncertainty maybe from lsqcurvefit that would be useful.
Cheers for the quick reply.

Jered Wells

Joseph, I'm not sure what you mean by calculating the error in P2. Without a ground truth, it's hard to say. If you expect a shift of say x0 based on knowledge of the original data, maybe you could compute the difference P2-x0? Otherwise, maybe you are referring to the uncertainty in P2? Let me know.

Joseph Horne

Hi Jared,
Probably really basic but I want to find the error in the peak position, P2. Can you tell me how?

Jered Wells

Alexandra, you need to define additional output variables in order to capture the fit parameters. For example, [YPRIME, PARAMS] = lorentzfit(x,y); should give you the parameters you are looking for. Also see the help documentation by calling >> help lotentzfit from the command line.

Alexandra Francisco

How can I access the fitted distribution parameters? Thank you.

Jonas Lederer

Hello Jered,
thanks for the file. It is very useful!
I just didn't understand how to extract the FWHM.
Thanks in advance

Jered Wells

Heather, thank you for your comment. If you need to fit a "double-Lorentzian function," you may consider modifying the code to do this. I suspect that fitting will be more unstable due to the increase in unknowns and the need to have more accurate initial seeds. I leave this exercise to you :-)


Thanks Jered for sharing the excellent file! Could there be a double-peak fitting function added? Thanks again.


Jered Wells

Sangeeta, please defer to the help documentation provided in the code. X and Y are the independent and dependent variables, respectively. Varargin is indicated because the user may choose to define a variable number of input parameters depending on his or her needs and applications.

Sangeeta Vinoth

what is varargin input?
I suppose and x and y are Wavelength and intensity data

ihsan AMIN

ihsan AMIN

Galya h

Tianhan Liu

This file is great and Jered is very helpful. It helped me save a lot of time analyzing data.

Jered Wells

Hamed, if you could provide me with some sample data, I could be more helpful. My contact information is stored in the MATLAB function just past the help documentation. Thanks


Dear Jered,

Thanks for sharing the code. It is really helpful.

In the application that I am interested in, I want to measure the quality factor of resonances. Meaning that the fit should perfectly follow the fast transition at the peak. But, when I use the code, it sort of smooths the response.
Could you please give me an idea how to resolve this problem?


Yea, Thanks for the script. It was very helpful

Jered Wells

Thank you, Ramu, for your comments. I trust that all of your questions have been answered.


I changed the 3 parameter fitting fucntion to
F= p(4) + (2*p(1)./pi)*(p(3)./(4*(x-p(2)).^2 + p(3).^2))

now p1 gives Area, p2 gives the center, p3 gives the FWHM and p4 gives the y offset.


I tried this function on my data and found that parameter calculated are very different from the one I get from Orgin.

I recalculated the width by taking the sq. root of P3. Is that the correct way ?

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!