IMSL_FCNLSQ

Syntax | Return Value | Arguments | Keywords | Discussion | Example | Errors | Version History

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

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

Syntax

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.

Arguments

f

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.

nbasis

Number of basis functions.

xdata

One-dimensional array containing the abscissas of the least-squares problem.

fdata

One-dimensional array containing the ordinates of the least-squares problem.

Keywords

DOUBLE

If present and nonzero, double precision is used.

INTERCEPT

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

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

WEIGHTS

Array of weights used in the least-squares fit.

Discussion

The IMSL_FCNLSQ function computes a best least-squares approximation to given univariate data of the form:

IMSL_FCNLSQ-55.jpg

by M basis functions:

IMSL_FCNLSQ-56.jpg

(where M = nbasis). In particular, the default for this function returns the coefficients a which minimize:

IMSL_FCNLSQ-57.jpg

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:

IMSL_FCNLSQ-58.jpg

Example

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 

Errors

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.

Version History

6.4

Introduced