lorentzfit(x,y,vara​rgin)

LORENTZFIT fits a single- or multi-parameter Lorentzian function to data
14.1K Downloads
Updated 15 Jun 2020

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.
%
% [YPRIME PARAMS RESNORM RESIDUAL JACOBIAN] = LORENTZFIT(X,Y) returns YPRIME(X)
% 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
%
% [...] = LORENTZFIT(X,Y,P0,BOUNDS,NPARAMS,OPTIONS) defines the OPTIONS
% 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 (2024). lorentzfit(x,y,varargin) (https://www.mathworks.com/matlabcentral/fileexchange/33775-lorentzfit-x-y-varargin), MATLAB Central File Exchange. Retrieved .

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!
Version Published Release Notes
1.7.0.0

Provides the Jacobian output by lsqcurvefit

1.6.0.0

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

1.5.0.0

Added INPUTCHECK and OPTIONS. Rearranged SWITCH loop.
Added OPTIONS to the LSQCURVEFIT routine.

1.4.0.0

Better identified default functionality in help file

1.3.0.0

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.

1.2.0.0

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

1.1.0.0

Cleaned up description

1.0.0.0