Least-squares fitting of EPR spectra, single and multiple components.
esfit(SimFcn,expSpc,Sys0,Vary,Exp); esfit(SimFcn,expSpc,Sys0,Vary,Exp,SimOpt); esfit(SimFcn,expSpc,Sys0,Vary,Exp,SimOpt,FitOpt); BestSys = esfit(...) [BestSys,BestSpc] = esfit(...) [BestSys,BestSpc,residuals] = esfit(...)
See also the user guide on fitting.
esfit fits EPR spectra simulated with
garlic,
pepper,
chili, etc. to experimental spectral data
using least-squares fitting algorithms.
If no output is requested, esfit starts an interactive user interface that allows extensive
control over the fitting process and also allows the export of the fitting results to the workspace. In the UI, the fitting algorithm can be changed,
parameters can be excluded from the fit, and multiple fit sets can be stored.
If the user interface is not desired, then esfit should be called with one, two, or three output arguments:
BestSys contains the spin system parameters of the best fit.
BestSpc contains the simulated spectrum that gives the best fit.
Residuals contains the vector of residuals between the simulated and the experimental spectrum.
The input parameters are the following:
SimFcn is the function handle of a simulation function: @garlic, @pepper,
@chili, @saffron, @salt, or a user-supplied custom function that makes use of one or more of these.
expSpc is a 1D array containing the experimental spectral data, which can be one data set or several data sets that have been concatenated (in the latter case, a custom simulation function would need to be defined for fitting).
Sys0 is a spin system structure containing all the parameters of the spin system.
For parameters that are to be varied during the fitting process, the values in the structure represent the centers of the search ranges.
Sys0 is a cell array of spin system structures, e.g. {Sys1,Sys2} for a two-component system. The weights for the different components are included in each component structure, e.g. Sys1.weight = 0.7 and Sys2.weight = 0.3. The weights are relative (0.7 and 0.3 is the same as 14 and 6) and can be included in the fit. If weight is omitted, it is assumed to be 1. There is no limit on the number of components.
Vary contains the approximate maximum variations for the spin system parameters allowed in the fitting. If a spin system parameter should be included
in the fitting, give it a non-zero value in this structure. If it should not be included, set its value in this structure to zero, or don't include it at all.
Sys0.lwpp = 5; Vary.lwpp = 2; % the linewidth is searched over a range of about 3 to 7 Vary.lwpp = 0; % the linewidth kept constant at the value given in Sys0For multiple components,
Vary should be a cell array containing one structure per component, e.g. {Vary1,Vary2} for two components. If none of the parameters of a component are varied in the fit, [] can be specified, e.g. {Vary1,[]} for a two-component fit where only parameters of the first component are varied.
Exp contains the experimental parameters needed for the simulation. Make sure to specify the field or frequency range you used for the experimental data in Exp.Range or Exp.mwRange. If you let the simulation function automatically pick a range, it will likely be different from the experimental one, and esfit cannot do its work. For example, if x is the experimental field range, use Exp.Range = [min(x) max(x)].
SimOpt contains simulation options that are forwarded directly to the simulation function. Refer to the documentation of
chili,
garlic,
pepper, and
salt for details.
FitOpt is a structure containing settings for the optimization algorithms used in esfit. The possible settings are discussed
further down on this page.
The structure FitOpt contains fitting options. The most important ones select the fitting algorithm and the function to use for computing the residuals:
FitOpt.MethodFitOpt.Method = 'simplex fcn'; FitOpt.Method = 'genetic int';
The keywords for choosing one of the available algorithms are
'simplex': Nelder-Mead simplex algorithm (unconstrained)
'levmar': Levenberg/Marquardt (unconstrained)
'montecarlo': Monte Carlo random search (constrained)
'genetic': Genetic algorithm (constrained)
'grid': Systematic grid search (constrained)
'swarm': Particle swarm algorithm (constrained)
'simplex' is used.
The second keyword specifies what the residuals and consequently the root-mean-square deviation (rmsd) should be computed from. Usually the residuals are computed by taking the difference of the experimental and the simulated spectrum. However, the spectra can be transformed before computing the residuals.
'fcn' means take the experimental and simulated spectral data directly.
'int': integrate the two spectra.
'dint': use the double integrals of the two spectra.
'diff': compute the derivatives of the two spectra.
'fft': FFT both spectra before computing the residuals.
'fcn'. If there are many resolved lines, 'int' gives better convergence.
FitOpt.Scaling'maxabs': make the maximum absolute of simulated and experimental spectrum equal
'minmax': rescales and shifts so that minimum and maximum of the two spectra are identical
'lsq': obtains the scaling factor by least-squares fitting, without shifting
'lsq0': same as 'lsq', but includes a constant baseline correction
'lsq1': same as 'lsq', but includes a linear baseline correction
'lsq2': same as 'lsq', but includes a quadratic baseline correction
'none': no scaling at all
If the baseline of the experimental spectra has been corrected beforehand, the best choice is 'maxabs'.
If the noise level is high, 'lsq' is a better choice:
FitOpt.Scaling = 'lsq';To include corrections for a baseline in the experimental spectrum, use
'lsq0, 'lsq1, or 'lsq2.
There are more options which are recognized by all fitting algorithms:
FitOpt.maxTimeesfit will terminate even if the fitting has not yet converged.
FitOpt.PrintLevelFitOpt.OutArg[nArgOut useArg] that tells esfit how to use the output arguments of the simulation function, which may only be required for advanced usage, e.g. when using a custom fitting function. nArgOut is the number of outputs provided by the function. esfit will call the function asking for this number of outputs. The second number, useArg, tells esfit which of the output arguments actually contains the simulated data.
E.g. FitOpt.OutArg = [3 2] indicates to call the function asking for three outputs and then to use output number 2 for the fitting.
For standard EasySpin simulation functions, OutArg is chosen automatically. For custom simulation functions that deviate in their output behavior, OutArg might be useful.
Each of the available fitting algorithms can be fine-tuned using a set of parameters.
Parameters for Nelder/Mead downhill simplex:
FitOpt.RandomStartVary. If set to 0, which is the default value, the center of the parameter space is used as the starting point.
FitOpt.TolEdgeLengthFitOpt.TolFunFitOpt.SimplexPars[rho chi psi sigma], where rho is the reflection coefficient, chi is the expansion coefficient, psi is the contraction coefficient, and sigma is the shrink coefficient. The default values are [1,2,0.5,0.5].
FitOpt.deltaParameters for Levenberg-Marquardt:
FitOpt.RandomStartVary. If set to 0, which is the default value, the center of the parameter space is used as the starting point.
FitOpt.TolStepFitOpt.lambdaFitOpt.deltaParameters for Monte Carlo:
FitOpt.nTrialsFitOpt.TolFunParameters for the genetic algorithm:
FitOpt.PopulationSizeFitOpt.maxGenerationsmaxGenerations can be decreased.
FitOpt.TolFun
Parameters for the grid search:
Parameters for the particle swarm:
Internally,
After rescaling,
For details about the algorithms implemented in
Here is a very simple example. Let's assume the experimental data are stored in
See the example section for a full example. Also, read the user guide about fitting.
FitOpt.GridSize
Vary.g = [0 0.001 0]; Vary.lw = 0.2;
Then GridSize can contain 1 or 2 numbers:
FitOpt.GridSize = 10; % 10 points for each parameter, making 100 grid points total
FitOpt.GridSize = [20 3]; % 10 points along g and 3 along lw, giving a total of 60
FitOpt.TolFunFitOpt.nParticlesFitOpt.TolFunFitOpt.TolStallIterFitOpt.SwarmParams[k w c1 c2]. k is the velocity limit
and determines how far a particle can move in an iteration (default 0.2). w is the inertial
coefficient (default 0.5) and describes the propensity of a particle to move in the same direction as the preivious
iteration. c1 is the cognitive coefficient (default 2) and determines to what
degree a particle moves towards the currently optimal point. c2 is the social coeffient (default 1)
and describes how much a particle follows the other particles. Together, k, c1
and c2 determine in which direction a particle will move in a given iteration.
esfit rescales the parameters to be fitted. E.g. if the linewidth should be fitted and is expected to be around 5 mT, plus minus 1.5 mT, then:
Sys.lw = 5;
Vary.lw = 1.5; esfit treats 5 mT as x = 0, 3.5 mT as -1 and 6.5 mT as +1.
esfit, see e.g.
expspc.
Then the following code performs a least-squares fitting using the Nelder/Mead downhill simplex algorithm.
Exp.mwFreq = 9.5;
Sys0.g = [2.1 2.2]
Sys0.lwpp = 0.1;
Vary.g = [0.05 0.02];
esfit(@pepper,expspc,Sys0,Vary,Exp);