|
IDL Analyst Reference Guide: Interpolation and Approximation |
|
The IMSL_FCNLSQ function computes a least-squares fit using user-supplied functions.
| Note This routine requires an IDL Analyst license. For more information, contact your ITT Visual Information Solutions sales or technical support representative. |
Result = IMSL_FCNLSQ(f, nbasis, xdata, fdata [, /DOUBLE] [, INTERCEPT=variable] [, SSE=variable] [, WEIGHTS=value])
A one-dimensional array containing the coefficients of the basis functions.
Scalar string specifying the name of a user-supplied function that defines the subspace from which the least-squares fit is to be performed. The k-th basis function evaluated at x is f (k, x), where k = 1, 2, ..., nbasis.
Number of basis functions.
One-dimensional array containing the abscissas of the least-squares problem.
One-dimensional array containing the ordinates of the least-squares problem.
If present and nonzero, double precision is used.
Named variable into which the coefficient of a constant function used to augment the user-supplied basis functions in the least-squares fit is stored. Setting this keyword forces an intercept to be added to the model.
Named variable into which the error sum of squares is stored.
Array of weights used in the least-squares fit.
The IMSL_FCNLSQ function computes a best least-squares approximation to given univariate data of the form:
by M basis functions:
(where M = nbasis). In particular, the default for this function returns the coefficients a which minimize:

where w = WEIGHTS, n = N_ELEMENTS (xdata), x = xdata, and f = fdata.
If the optional keyword INTCERCEPT is used, then an intercept is placed in the model and the coefficients a, returned by IMSL_FCNLSQ, minimize the error sum of squares as indicated below:

In this example, the following function is fit:
1 + sinx + 7sin3x
This function is evaluated at 90 equally spaced points on the interval [0, 6]. Four basis functions, 1, sinx, sin2x, and sin3x, are used.
.RUN
; Define the basis functions.
FUNCTION f, k, x
IF (k EQ 1) THEN RETURN, 1. $
ELSE RETURN, SIN((k - 1) * x)
END
n = 90
xdata = 6 * FINDGEN(n)/(n - 1)
fdata = 1 + SIN(xdata) + 7 * SIN(3 * xdata)
nbasis = 4
; Generate the data.
coefs = IMSL_FCNLSQ('f', nbasis, xdata, fdata)
; Compute the coefficients summing IMSL_FCNLSQ.
PM, coefs, FORMAT = '(f10.5)'
; Print the results.
1.00000
1.00000
0.00000
7.00000
MATH_LINEAR_DEPENDENCE—Linear dependence of the basis functions exists. One or more components of coef are set to zero.
MATH_LINEAR_DEPENDENCE_CONST—Linear dependence of the constant function and basis functions exists. One or more components of coef are set to zero.
MATH_NEGATIVE_WEIGHTS_2—All weights must be greater than or equal to zero.
IDL Online Help (March 06, 2007)