Data-fitting

Header file datafit.h

Functions

See also methods to fit surfaces to data.

Polynomial fitting

lsqline
Finds best-fitting straight line and standard deviation, with intercept calculated or specified. Equation is of the form y = a.x + b.
int lsqline(
   // Data in:
   const Vector& x, // ordinates
   const Vector& y, // y(x) data values
   const int fixb, // 1: define bhat / 0: fit it
   const double eps, // numerical bandwidth to prevent overflow on division
   // Data in/out:
   double& bhat, // best-fitting (or supplied) intercept
   // Data out:
   double& ahat, // best-fitting gradient
   double& sd // standard deviation
)
Returns 1 on success; 0 if failed because gradient or intercept calculation would involve division by small number.
polyfitg
Solves overdetermined system of equations by Givens rotations.
Array polyfitg(
   const Array& x, // Array of x-ordinates
   const Array& y, // Array of y-values
   const Array& w, // Array of weights
   const int n, // number of polynomial coefficients, i.e. order + 1
   double& res, // residual of l2 fit (returned value)
   const double eps = 1.0e-6 // numerical bandwidth for spotting singular matrix
)
Returns polynomial coefficients (order of increasing powers of x), or an Array of length zero if it fails.

Multilinear fitting

natural fit to data defined at the corners of a rectangle
Array frectfit(
   const Array& x, // x ordinates of corners (2)
   const Array& y, // y ordinates of corners (2)
   const matrix& f, // function values (2x2)
   const eps = 1.0e-6 // numerical bandwidth
)
Returns coefficients for the functions {1,x,y,xy} respectively, or an Array of no elements if the fitting fails.

Utilities

Work out which run of points to use for fitting tabular data
int fitstart(
   const Array& r, // Array of ordinates
   const double rr, // value around which fit is to be performed
   const int nr // number of points required for the fit
)
(returns index of first point)