>  Docs Center  >  IDL Reference  >  Advanced Math and Stats  >  IMSL_FCNLSQ



The IMSL_FCNLSQ function computes a least-squares fit using user-supplied functions.

This routine requires an IDL Advanced Math and Stats license. For more information, contact your sales or technical support representative.

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.

; Define the basis functions. FUNCTION f, k, x
IF (k EQ 1) THEN RETURN, 1. $
  ELSE RETURN, SIN((k - 1) * x)
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)'

IDL prints:



Warning Errors

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.

Fatal Errors

MATH_NEGATIVE_WEIGHTS_2: All weights must be greater than or equal to zero.


Result = IMSL_FCNLSQ(F, Nbasis, Xdata, Fdata [, /DOUBLE] [, INTERCEPT=variable] [, SSE=variable] [, WEIGHTS=value])

Return 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.


DOUBLE (optional)

If present and nonzero, double precision is used.

INTERCEPT (optional)

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.

SSE (optional)

Named variable into which the error sum of squares is stored.

WEIGHTS (optional)

Array of weights used in the least-squares fit.

Version History



© 2020 Harris Geospatial Solutions, Inc. |  Legal
My Account    |    Store    |    Contact Us