View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.8 | 7 ratings Rate this file 124 Downloads (last 30 days) File Size: 28 KB File ID: #33775 Version: 1.6
image thumbnail




15 Nov 2011 (Updated )

LORENTZFIT fits a single- or multi-parameter Lorentzian function to data

| Watch this File

File Information

% 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
% and RESIDUAL 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] = 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] = lorentzfit(x,y,[],[],'3');
% figure; plot(x,y,'b.','LineWidth',2)
% hold on; plot(x,yprime2,'r-','LineWidth',2)
% See also: lsqcurvefit.

MATLAB release MATLAB 8.5 (R2015a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (18)
18 Feb 2017 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.

Comment only
18 Feb 2017 Alexandra Francisco

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

07 Dec 2016 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

Comment only
13 Oct 2016 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 :-)

Comment only
13 Oct 2016 heather

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

Comment only
11 Oct 2016 heather

22 Jul 2016 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.

Comment only
22 Jul 2016 Sangeeta Vinoth

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

Comment only
24 May 2016 ihsan AMIN

24 May 2016 ihsan AMIN

06 Jan 2016 Galya h

31 Jul 2015 Tianhan Liu

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

18 Jun 2015 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

Comment only
17 Jun 2015 Hamed

Hamed (view profile)

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?

Comment only
17 Apr 2012 Ramu

Ramu (view profile)

Yea, Thanks for the script. It was very helpful

Comment only
17 Apr 2012 Jered Wells

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

Comment only
17 Apr 2012 Ramu

Ramu (view profile)

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.

Comment only
17 Apr 2012 Ramu

Ramu (view profile)

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 ?

13 Feb 2012 1.1

Cleaned up description

17 Feb 2012 1.2

Documentation updated to match MATLAB standard for help file
H1 line has been included
Error checking included
Outputs modified for easier use

09 Mar 2012 1.3

Included option to select one, two, or three parameter Lorentzian model with or without constant parameter.
Improved documentation
Provided two additional outputs from LSQCURVEFIT: RESNORM and RESIDUAL.

17 Apr 2012 1.4

Better identified default functionality in help file

22 Jul 2015 1.5

Added INPUTCHECK and OPTIONS. Rearranged SWITCH loop.

22 Jul 2015 1.5

Added OPTIONS to the LSQCURVEFIT routine.

31 Jul 2015 1.6

Added stopping thresholds for fitting based on magnitude of input data.

Contact us