Non linear (constrained) parametric fit of measured (weighted) data
[p, fmin, status] = datafit([iprint,] G [,DG], Data [,Wd] [,Wg][,'b',p_inf,p_sup], p0 [,algo][,stop])
scalar used to set the trace mode:
Value | Action |
---|---|
0 | nothing (except errors) is reported |
1 | initial and final reports |
2 | adds a report per iteration |
>2 | adds reports on linear search |
![]() | Most of the reports are displayed in the Scilab console. |
p_inf, p_sup
sequence, where
p_inf
and p_sup
are real matrices
with the p0
shape and sizes.
p_inf
is the vector of lower bounds of respective
p
components. p_sup
are upper bounds.
p0
sizes.
Data(:,i)
are the #nzc coordinates of the ith measurement.
Gap function: handle of a function in Scilab language or of a compiled external function. Computed gap(s) between the fitting model and data may be of the first order, not quadratic.
If G()
does not need
any input parameters (like constants), only arguments, its syntax must be
g = G(p, Data)
.
If G()
needs
some input parameters in addition to input arguments, its syntax must be
g = G(p, Data, param1, param2, .., paramN)
.
Then, list(G, param1, param2, .., paramN)
must be provided
to datafit()
instead of only G
.
The following definitions will be used:
ng = size(g,1)
: number of fitting criteria (= of
gap definitions).np = size(p,"*")
: total number of fitting parameters.nd = size(Data,2)
: number of data points.ndc = size(Data,1)
: number of coordinates of each
data point.Then, the sizes of G arguments are the following ones:
g | : (ng x nd) matrix
|
---|---|
p | : fitting parameters to be optimized. |
Data | : (ndc x nd) matrix: Each column provides the
#ndc coordinates of a given data point.
If G computes gaps only for a single
data point, datafit() will automatically
loop over the set of Data columns.
This makes datafit() slower. |
G
wrt p
.
Its syntax must be like S = DG(p, Data)
. The expected sizes
of the result are (ng x np x 1) (Data-unvectorized) or (ng x np x nd)
(Data-vectorized).
G()
defines as many types of gaps as required.
Most often, a unique gap will be computed -- most often the vertical one --,
but other ones can be computed (horizontal, nearest distance, etc).
This matrix allows to weight the different types.
'qn'
(quasi-Newton, default), or
'gc'
(conjugate gradient), or
'nd'
(non-differentiable. Then p_inf
and p_sup
parameters bounds are not accepted).
Selects the algorithm used to perform the fitting.
See optim()
for more details.
sequence of optional arguments controlling the convergence of the fitting algorithm:
'ar',nap, [iter [,epsg [,epsf [,epsx]]]]
, with
"ar" | reserved keyword heading the list of stopping criteria as defined below. |
---|---|
nap | maximum number of calls to the cost f function allowed (default: 100). |
iter | maximum number of iterations allowed (default: 100). |
epsg | threshold on gradient norm (default: %eps). |
epsf | threshold controlling the decreasing of f
(defined in the description section). Default: 0. |
epsx | threshold controlling the variation of p .
This vector (possibly matrix) of same size as p0
can be used to scale p . Default: 0. |
sqrt(fmin/ng/nd)
or
sqrt(fmin/ng/sum(Wd))
, where fmin is the quadratic residue
of the cost function f
(see the description).
9
is successful. Possible values are:
1 | : Norm of projected gradient lower than... | .... | 6 | : Optim stops: too small variations in gradient direction. |
---|---|---|---|---|
2 | : At last iteration, f decreases by less than... | 7 | : Stop during calculation of descent direction. | |
3 | : Optim stops: Too small variations for p. | 8 | : Stop during calculation of estimated hessian. | |
4 | : Optim stops: maximum number of calls to f is reached. | 9 | : Successful completion. | |
5 | : Optim stops: maximum number of iterations is reached. | 10 | : Linear search fails. |
Other optim()
input arguments such that df0
,
mem
, or 'in'
may be used with
datafit()
. They will be passed as is to optim()
.
datafit
is used to fit a parametrized model to given data.
A function G(p, Data)
must be defined to compute the gaps between the
Data
points and a model whose parameters to be tuned are provided
through the matrix p
.
datafit()
checks whether G()
is vectorized or not
over Data
. If G()
works only with a single
data
point as a single column of coordinates, then datafit()
loops over data=Data(:,i)
points. Otherwise, G(p, Data)
is called for the whole Data
array of data coordinates, which is
way faster if G()
's code is truly vectorized.
Starting from initial values p0
of the model parameters,
datafit()
varies them in order to minimize the whole Model-to-Data
distance defined as
f = (G(p, Data(:,1))' * Wg * G(p, Data(:,1)))* Wd(1) + .. (G(p, Data(:,2))' * Wg * G(p, Data(:,2)))* Wd(2) + .. ... (G(p, Data(:,nz))' * Wg * G(p,Data(:,nz)))* Wd(nz)
When Wg = eye()
(default), this definition is equivalent to
f = sum(G(p, Data(:,1)).^2) * Wd(1) + .. sum(G(p, Data(:,2)).^2) * Wd(2) + .. ... sum(G(p, Data(:,nz)).^2) * Wd(nz)
If in addition G
returns only one gap type, this even simplifies to
f = G(p, Data(:,1))^2 * Wd(1) + .. G(p, Data(:,2))^2 * Wd(2) + .. ... G(p, Data(:,nz))^2 * Wd(nz)
datafit()
calls optim()
to minimize f.
Choosing appropriately p0
may improve and faster
datafit()
's convergence to the best fit.
Simple example: Polynomial fitting (parabola = 3 parameters).
|
![]() | ||||
Fitting a gaussian curve + sloping base (5 parameters):
|
![]() | ||||
Extraction of 3 overlapping gaussian curves (9 parameters): example with constrained bounds and a matrix of parameters.
|
![]() | ||||
With a parametric curve: Fitting a off-centered tilted 2D elliptical arc (5 parameters).
|
![]() |
Version | Description |
6.1 | New option The gap function G() may now be vectorized over Initial parameters The New output argument |