1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
.help nlfit Feb91 "Math Package"
.ih
NAME
nlfit -- Levenberg-Marquardt non-linear least-squares fitting package
.ih
SYNOPSIS
The principal entry points into the NLFIT package are listed below.
.nf
nlinit - Initialize the fitting routines
nlstat - Get the value of an NLFIT parameter
nlpget - Get the values of the fitted parameters
nlfree - Free memory allocated by nlinit
nlfit - Fit the function
nleval - Evaluate the curve at a given point
nlvector - Evaluate the curve at an array of points
nlerrors - Compute the fits statistics and errors in the parameters
.fi
The calling sequences for the above routines are listed below. The [ird]
stand for integer, real and double precision versions of each routine
respectively.
.nf
nlinit[rd] (nl, address(fnc), address(dfnc), params, dparams,
nparams, plist, nfparams, tol, itmax)
ival = nlstati (nl, param)
[rd]val = nlstat[rd] (nl, param)
nlpget[rd] (nl, params, nparams)
nlfree[rd] (nl)
nlfit[rd] (nl, x, z, w, npts, nvars, wtflag, ier)
[rd]val = nleval[rd] (nl, x, nvars)
nlvector[rd] (nl, x, zfit, npts, nvars)
nlerrors[rd] (nl, z, zfit, wts, npts, variance, chisqr, errors)
.fi
The user supplied functions fnc and dfnc must have the following form.
.nf
fnc (x, nvars, params, nparams, zfit)
dfnc (x, nvars, params, dparams, nparams, zfit, derivs)
.fi
The addresses of these functions can be obtained by a call to lcopr as
follows.
.nf
address = locpr (fnc)
.fi
.ih
DESCRIPTION
The NLFIT package provides a set of routines for fitting data to non-linear
functions of several variables using least squares techniques.
NLFIT uses the Levenberg-Marquardt
method to solve for the parameters of a user specified non-linear equation.
The user must supply two subroutines. The first subroutine evaluates the
function in terms of its parameters. The second subroutine evaluates the
function and its derivatives in terms of its parameters. The user must
also supply initial
guesses for the parameters and parameter increments, the list of
parameters to be varied during the fitting process, a fitting
tolerance and the maximum number of iterations.
.ih
NOTES
The poackage definitions for NLFIT can be found in the file
lib$math/nlfit.h. In order to make these definitions available
to the calling program, the user must insert the statement
"include <math/nlfit.h>" inside the calling program.
The permitted values for the param argument in nlstat[ird] are the following.
.nf
define NLNPARAMS integer # Number of parameters
define NLNFPARAMS integer # Number of fitted parameters
define NLITMAX integer # Maximum number of iterations
define NLITER integer # Current number of iterations
define NLNPTS integer # Number of points
define NLSUMSQ real/double # Current reduced chi-squared
define NLOLDSQ real/double # Previous reduced chi-squared
define NLLAMBDA real/double # Value of lambda factor
define NLTOL real/double # Fitting tolerance in %chi-squared
define NLSCATTER real/double # Mean scatter in the fit
.fi
The permitted values of the wtflag argument in nlfit[rd] are the following.
.nf
define WTS_USER # User enters weights
define WTS_UNIFORM # Equal weights
define WTS_CHISQ # Chi-squared weights
define WTS_SCATTER # Weights include scatter term
.fi
The permitted error values returned from nlfit[rd] are the following.
.nf
define DONE 0 # Solution converged
define SINGULAR 1 # Singular matrix
define NO_DEG_FREEDOM 2 # Too few points
define NOT_DONE 3 # Solution did not converge
.fi
.ih
REFERENCES
1. Bevington,P.R., 1969, Data Reduction and Error Analysis for the Physical
Sciences, Chapter 11, page 235.
2. Press, W.H. et al., 1986, Numerical Recipes: The Art of Scientific
Computing, Chapter 14, page 523
.ih
EXAMPLES
.nf
Example 1: Fit a curve to the data using uniform weighting. Fit all the
parameters.
# Include nlfit definitions
include <math/nlfit.h>
# Declare the variables
int nparams, nfparams, itmax, npts, nvars, ier
int plist[nparams]
real tol, variance, chisqr
real params[nparams], dparams[nparams], vars[nvars,npts], z[npts]
real w[npts], zfit[npts], errors[nparams]
# Declare the functions
extern fncname(), dfncname()
int locpr()
begin
... get data, set initial values of all parameters, parameter
... increments, tol and itmax
# Define list of variables to be fitted
nfparams = nparams
do i = 1, nparams
plist[i] = i
# Fit only parameters 1,3 and 5
#nfparams = 3
#plist[1] = 1
#plist[2] = 3
#plist[3] = 5
# Initialize the fitting routines
call nlinitr (nl, locpr (fncname), locpr (dfncname),
params, dparams, nparams, plist, nfparams, tol, itmax)
# Fit the data
call nlfitr (nl, x, z, w, npts, nvars, WTS_UNIFORM, ier)
if (ier != DONE)
... take error action
# Compute the statistics of the fit
call nlvectorr (nl, x, zfit, npts, nvars)
call nlerrorsr (nl, z, zfit, wts, npts, variance, chisqr,
errors)
# Get the values of the fitted parameters
call nlpgetr (nl, params, nparams)
call nlfreer (nl)
# Print the parameters and their errors.
do i = 1, nparams {
call printf ("%d %g %g\n")
call pargi (i)
call pargr (params[i])
call pargr (errors[i])
}
end
.fi
.endhelp
|