fit(FUN) fits the active curve with the function FUN. See below for the
syntax of FUN. If FUN is not specified, 'linear' is used.
fit(X,Y,FUN) or fit(Y,FUN) fit the data (X,Y) (or Y) with the function
FUN (see below for the syntax of FUN). X and Y must be vectors of
equal length. If X is not specified, X=[1, 2, 3...] is used.
Note that fit only computes the coefficients, but does not display the
fit. Use showfit to display the fit, or selectfit to fit only a part of
the current curve.
By default, the first curve in the active figure is used (see fitparam
to change this default behavior). To fit another curve, select it
before calling fit.
The function string FUN can be:
- the name of a predefined fitting function (see below).
- the name of a user-defined fitting function (see editfit).
- an equation, in the form 'y(x)=...', where 'x' represents the
X-data, and all the other variables are parameters to be fitted
('a', 'x_0', 'tau', ...). If the left-hand-side 'y(x)' is not
specified, 'x' is taken for the X-Data. All the parameter names
are accepted, except Matlab reserved strings ('sin', 'pi', ...)
The predefined fitting functions are:
- linear y = m * x
- affine or poly1 y = a*x + b
- poly{n} y = a0 + a1 * x + ... + an * x^n
- power y = c*x^n
- sin y = a * sin (b * x)
- cos y = a * cos (b * x)
- exp y = a * exp (b * x)
- log y = a * log (b * x)
- cngauss y = exp(-x^2/(2*s^2))/(2*pi*s^2)^(1/2)
- cfgauss y = a*exp(-x^2/(2*s^2))
- ngauss y = exp(-(x-x0)^2/(2*s^2))/(2*pi*s^2)^(1/2)
- gauss y = a*exp(-(x-x0)^2/(2*s^2))
('ngauss' is a 2-parameters normalized Gaussian, and 'gauss' is a
3-parameters non-normalized (free) Gaussian. 'cngauss' and 'cfgauss'
are centered normalized/free Gaussian.)
By default, all the starting guesses for the coefficients are taken as 1.
However, nonlinear fits often require to specify the starting guesses
(it is sufficient to choose values that have the correct sign and correct
order of magnitude, eg 0.01, 1, 100...). The starting guesses for the
coefficients of the fit may be specified in two ways:
- directly in the string FUN, after the fit definition, eg:
'c0 + a*sin(pi*x/lambda); c0=1; a=0.1; lambda=100'
('!' or '$' may also be used instead of ';').
- by specifying them as an additional input argument for fit, eg:
fit(x,y,'c0 + a*sin(pi*x/lambda)',[0.1 1 100]);
(note that in this case the parameters must be ordered alphabetically).
By default, Y is fitted in linear mode. If you want to fit LOG(Y)
instead, you must specify the option 'log' to the string FUN, separeted
by the symbol ';' or '$' or '!' (eg, FUN='a*x^n;log'). This is
specially useful to fit power laws with equally weighted points in a
log scale. If nothing specified, the option 'lin' is used.
Example: plotsample('power'), and compare
fit('power;lin') and fit('power;log')
F = fit(...) does the same, but also returns a structure F having the
following fields:
- name name of the fit
- eq equation of the fit
- param cell array of strings: names of the parameters
- m values of the coefficients
- m0 initial guess for the coefficients
- r correlation coefficient R (Pearson's correlation)
- fitmode 'lin' (y is fitted) or 'log' (log(y) is fitted) mode
This structure F can be further used with showfit, selectfit, dispeqfit,
showeqbox, makevarfit and editcoeff.
From F, you can get the values of the fitted parameters. If you want to
create in the current Matlab workspace the variables associated to
these parameters, use makevarfit (or see the option 'automakevarfit' in
fitparam).
The correlation coefficient R is defined as (SSR/(SSE+SSR))^(1/2), where
SSR = sum ((y_fit - mean(y)).^2) % sum of squared residuals
SSE = sum ((y_fit - y).^2) % sum of squared errors
(see http://mathworld.wolfram.com/CorrelationCoefficient.html)
Examples:
plotsample('damposc');
f = fit('u(t) = c + u_a * sin(2*pi*t/T) * exp(-t/tau); T=5; tau=20');
showfit(f);
plotsample('poly2');
[x,y] = pickdata;
f = fit(x, y, 'z(v) = poly3');
editcoeff(f);
plotsample('poly2');
f = fit('beta(z) = poly2');
showfit(f, 'fitcolor', 'red', 'fitlinewidth', 2);
See Also
selectfit, showfit, plotsample, dispeqfit, editcoeff,
FMINSEARCH, makevarfit, fitparam.