## Multi-parameter fitting

The functions described in this section perform least-squares fits to a general linear model, @math{y = X c} where @math{y} is a vector of @math{n} observations, @math{X} is an @math{n} by @math{p} matrix of predictor variables, and @math{c} are the @math{p} unknown best-fit parameters, which are to be estimated.

The best-fit is found by minimizing the weighted sums of squared residuals, @math{\chi^2},

with respect to the parameters @math{c}. The weights are specified by the diagonal elements of the @math{n} by @math{n} matrix @math{W}. For unweighted data @math{W} is replaced by the identity matrix.

This formulation can be used for fits to any number of functions and/or variables by preparing the @math{n}-by-@math{p} matrix @math{X} appropriately. For example, to fit to a @math{p}-th order polynomial in x, use the following matrix,

where the index @math{i} runs over the observations and the index @math{j} runs from 0 to @math{p-1}.

To fit to a set of @math{p} sinusoidal functions with fixed frequencies @math{\omega_1}, @math{\omega_2}, ..., @math{\omega_p}, use,

To fit to @math{p} independent variables @math{x_1}, @math{x_2}, ..., @math{x_p}, use,

where @math{x_j(i)} is the @math{i}-th value of the predictor variable @math{x_j}.

The functions described in this section are declared in the header file `gsl_multifit.h'.

The solution of the general linear least-squares system requires an additional working space for intermediate results, such as the singular value decomposition of the matrix @math{X}.

Function: gsl_multifit_linear_workspace * gsl_multifit_linear_alloc (size_t n, size_t p)
This function allocates a workspace for fitting a model to n observations using p parameters.

Function: void gsl_multifit_linear_free (gsl_multifit_linear_workspace * work)
This function frees the memory associated with the workspace w.

Function: int gsl_multifit_linear (const gsl_matrix * X, const gsl_vector * y, gsl_vector * c, gsl_matrix * cov, double * chisq, gsl_multifit_linear_workspace * work)
This function computes the best-fit parameters c of the model @math{y = X c} for the observations y and the matrix of predictor variables X. The variance-covariance matrix of the model parameters cov is estimated from the scatter of the observations about the best-fit. The sum of squares of the residuals from the best-fit, @math{\chi^2}, is returned in chisq.

The best-fit is found by singular value decomposition of the matrix X using the preallocated workspace provided in work. The modified Golub-Reinsch SVD algorithm is used, with column scaling to improve the accuracy of the singular values. Any components which have zero singular value (to machine precision) are discarded from the fit.

Function: int gsl_multifit_wlinear (const gsl_matrix * X, const gsl_vector * w, const gsl_vector * y, gsl_vector * c, gsl_matrix * cov, double * chisq, gsl_multifit_linear_workspace * work)

This function computes the best-fit parameters c of the model @math{y = X c} for the observations y and the matrix of predictor variables X. The covariance matrix of the model parameters cov is estimated from the weighted data. The weighted sum of squares of the residuals from the best-fit, @math{\chi^2}, is returned in chisq.

The best-fit is found by singular value decomposition of the matrix X using the preallocated workspace provided in work. Any components which have zero singular value (to machine precision) are discarded from the fit.