The CURVEFIT function uses a gradient-expansion algorithm to compute a non-linear least squares fit to a user-supplied function with an arbitrary number of parameters. The user-supplied function may be any non-linear function where the partial derivatives are known or can be approximated. Iterations are performed until the chi square changes by a specified amount, or until a maximum number of iterations have been performed. The CURVEVIT function returns a vector of values for the dependent variables, as fitted by the function fit.

This routine is written in the IDL language. Its source code can be found in the file ```
curvefit.pro
```

in the ```
lib
```

subdirectory of the IDL distribution.

For instrumental (Gaussian) weighting, set *
Weights*
_{
i}
= 1.0/standard_deviation(*
Y*
_{
i}
)^{
2}
. For statistical (Poisson) weighting, *
Weights*
_{
i}
= 1.0/*
Y*
_{
i}
. For no weighting, set *
Weights*
_{
i}
= 1.0.

A vector with as many elements as the number of terms in the user-supplied function, containing the initial estimate for each parameter. On return, the vector A contains the fitted model parameters. If *
A*
is double-precision, calculations are performed in double-precision arithmetic, otherwise they are performed in single-precision arithmetic.

Use this keyword to specify the name of the function to fit. If this keyword is omitted, CURVEFIT assumes that the IDL procedure ```
FUNCT
```

is to be used. If ```
FUNCT
```

is not already compiled, IDL compiles the function from the file ```
funct.pro
```

, located in the ```
lib
```

subdirectory of the IDL distribution. ```
FUNCT
```

evaluates the sum of a Gaussian and a second-order polynomial.

The function to be fit must be written as an IDL procedure and compiled prior to calling CURVEFIT. The procedure must accept values of *
X*
(the independent variable), and *
A*
(the fitted function's initial parameter values). It must return values for *
F*
(the function's value at *
X*
), and optionally *
PDER*
(a 2D array of partial derivatives).

Set this keyword equal to a named variable that will contain the actual number of iterations performed.

Fit a function of the form ```
F(x) = a * exp(b*x) + c
```

to sample pairs contained in arrays *
X*
and *
Y*
. The partial derivatives are easily computed symbolically:

First, define a procedure to return ```
F(x)
```

and the partial derivatives, given *
X*
. Note that A is an array containing the values ```
a
```

, ```
b
```

, and ```
c
```

.

IF N_PARAMS() GE 4 THEN $ *;
If the procedure is called with four parameters, calculate the partial derivatives.*

pder= [[bx], [A[0] * X * bx], [replicate(1.0, N_ELEMENTS(X))]][

Compute the fit to the function we have just defined. First, define the independent and dependent variables:

Y = [12.0, 11.0, 10.2, 9.4, 8.7, 8.1, 7.5, 6.9, 6.5, 6.1]

weights = 1.0/Y *;
Define a vector of weights.*

A = [10.0,-0.1,2.0] *;
Provide an initial guess of the function's parameters.*

yfit = CURVEFIT(X, Y, weights, A, SIGMA, FUNCTION_NAME='gfunct')

*;
Compute the parameters.*

PRINT, 'Function parameters: ', A *;
Print the parameters returned in A.*

Function parameters: 9.91120 -0.100883 2.07773