e04gd is a comprehensive modified Gauss–Newton algorithm for finding an unconstrained minimum of a sum of squares of m nonlinear functions in n variables mn. First derivatives are required.
The method is intended for functions which have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities).

Syntax

C#
public static void e04gd(
	int m,
	int n,
	E04..::..E04GD_LSQFUN lsqfun,
	E04..::..E04GD_LSQMON lsqmon,
	int iprint,
	int maxcal,
	double eta,
	double xtol,
	double stepmx,
	double[] x,
	out double fsumsq,
	double[] fvec,
	double[,] fjac,
	double[] s,
	double[,] v,
	out int niter,
	out int nf,
	out int ifail
)
Visual Basic
Public Shared Sub e04gd ( _
	m As Integer, _
	n As Integer, _
	lsqfun As E04..::..E04GD_LSQFUN, _
	lsqmon As E04..::..E04GD_LSQMON, _
	iprint As Integer, _
	maxcal As Integer, _
	eta As Double, _
	xtol As Double, _
	stepmx As Double, _
	x As Double(), _
	<OutAttribute> ByRef fsumsq As Double, _
	fvec As Double(), _
	fjac As Double(,), _
	s As Double(), _
	v As Double(,), _
	<OutAttribute> ByRef niter As Integer, _
	<OutAttribute> ByRef nf As Integer, _
	<OutAttribute> ByRef ifail As Integer _
)
Visual C++
public:
static void e04gd(
	int m, 
	int n, 
	E04..::..E04GD_LSQFUN^ lsqfun, 
	E04..::..E04GD_LSQMON^ lsqmon, 
	int iprint, 
	int maxcal, 
	double eta, 
	double xtol, 
	double stepmx, 
	array<double>^ x, 
	[OutAttribute] double% fsumsq, 
	array<double>^ fvec, 
	array<double,2>^ fjac, 
	array<double>^ s, 
	array<double,2>^ v, 
	[OutAttribute] int% niter, 
	[OutAttribute] int% nf, 
	[OutAttribute] int% ifail
)
F#
static member e04gd : 
        m : int * 
        n : int * 
        lsqfun : E04..::..E04GD_LSQFUN * 
        lsqmon : E04..::..E04GD_LSQMON * 
        iprint : int * 
        maxcal : int * 
        eta : float * 
        xtol : float * 
        stepmx : float * 
        x : float[] * 
        fsumsq : float byref * 
        fvec : float[] * 
        fjac : float[,] * 
        s : float[] * 
        v : float[,] * 
        niter : int byref * 
        nf : int byref * 
        ifail : int byref -> unit 

Parameters

m
Type: System..::..Int32
On entry: the number m of residuals, fix, and the number n of variables, xj.
Constraint: 1nm.
n
Type: System..::..Int32
On entry: the number m of residuals, fix, and the number n of variables, xj.
Constraint: 1nm.
lsqfun
Type: NagLibrary..::..E04..::..E04GD_LSQFUN
lsqfun must calculate the vector of values fix and Jacobian matrix of first derivatives fixj at any point x. (However, if you do not wish to calculate the residuals or first derivatives at a particular x, there is the option of setting a parameter to cause e04gd to terminate immediately.)

A delegate of type E04GD_LSQFUN.

Note:  lsqfun should be tested separately before being used in conjunction with e04gd.
lsqmon
Type: NagLibrary..::..E04..::..E04GD_LSQMON
If iprint0, you must supply lsqmon which is suitable for monitoring the minimization process. lsqmon must not change the values of any of its parameters.
If iprint<0, the dummy method E04FDZ can be used as lsqmon.

A delegate of type E04GD_LSQMON.

Note:  you should normally print the sum of squares of residuals, so as to be able to examine the sequence of values of Fx mentioned in [Accuracy]. It is usually also helpful to print xc, the gradient of the sum of squares, niter and nf.
iprint
Type: System..::..Int32
On entry: the frequency with which lsqmon is to be called.
iprint>0
lsqmon is called once every iprint iterations and just before exit from e04gd.
iprint=0
lsqmon is just called at the final point.
iprint<0
lsqmon is not called at all.
iprint should normally be set to a small positive number.
Suggested value: iprint=1.
maxcal
Type: System..::..Int32
On entry: enables you to limit the number of times that lsqfun is called by e04gd. There will be an error exit (see [Error Indicators and Warnings]) after maxcal evaluations of the residuals (i.e., calls of lsqfun with iflag set to 2). It should be borne in mind that, in addition to the calls of lsqfun which are limited directly by maxcal, there will be calls of lsqfun (with iflag set to 1) to evaluate only first derivatives.
Suggested value: maxcal=50×n.
Constraint: maxcal1.
eta
Type: System..::..Double
On entry: every iteration of e04gd involves a linear minimization, i.e., minimization of Fxk+αkpk with respect to αk. eta specifies how accurately these linear minimizations are to be performed. The minimum with respect to αk will be located more accurately for small values of eta (say, 0.01) than for large values (say, 0.9).
Although accurate linear minimizations will generally reduce the number of iterations, they will tend to increase the number of calls of lsqfun (with iflag set to 2) needed for each linear minimization. On balance it is usually efficient to perform a low accuracy linear minimization.
Suggested value: eta=0.5 (eta=0.0 if n=1).
Constraint: 0.0eta<1.0.
xtol
Type: System..::..Double
On entry: the accuracy in x to which the solution is required.
If xtrue is the true value of x at the minimum, then xsol, the estimated position before a normal exit, is such that
xsol-xtrue<xtol×1.0+xtrue
where y=j=1nyj2. For example, if the elements of xsol are not much larger than 1.0 in modulus and if xtol=1.0E−5, then xsol is usually accurate to about five decimal places. (For further details see [Accuracy].)
If Fx and the variables are scaled roughly as described in [Further Comments] and ε is the machine precision, then a setting of order xtol=ε will usually be appropriate. If xtol is set to 0.0 or some positive value less than 10ε, e04gd will use 10ε instead of xtol, since 10ε is probably the smallest reasonable setting.
Constraint: xtol0.0.
stepmx
Type: System..::..Double
On entry: an estimate of the Euclidean distance between the solution and the starting point supplied by you. (For maximum efficiency, a slight overestimate is preferable.) e04gd will ensure that, for each iteration,
j=1nxjk-xjk-12stepmx2
where k is the iteration number. Thus, if the problem has more than one solution, e04gd is most likely to find the one nearest to the starting point. On difficult problems, a realistic choice can prevent the sequence of xk entering a region where the problem is ill-behaved and can help avoid overflow in the evaluation of Fx. However, an underestimate of stepmx can lead to inefficiency.
Suggested value: stepmx=100000.0.
Constraint: stepmxxtol.
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: x[j-1] must be set to a guess at the jth component of the position of the minimum, for j=1,2,,n.
On exit: the final point xk. Thus, if ifail=0 on exit, x[j-1] is the jth component of the estimated position of the minimum.
fsumsq
Type: System..::..Double%
On exit: the value of Fx, the sum of squares of the residuals fix, at the final point given in x.
fvec
Type: array<System..::..Double>[]()[][]
An array of size [m]
On exit: the value of the residual fix at the final point given in x, for i=1,2,,m.
fjac
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: dim1m
On exit: the value of the first derivative fixj evaluated at the final point given in x, for i=1,2,,m and j=1,2,,n.
s
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: the singular values of the Jacobian matrix at the final point. Thus s may be useful as information about the structure of your problem.
v
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: dim1n
On exit: the matrix V associated with the singular value decomposition
J=USVT
of the Jacobian matrix at the final point, stored by columns. This matrix may be useful for statistical purposes, since it is the matrix of orthonormalized eigenvectors of JTJ.
niter
Type: System..::..Int32%
On exit: the number of iterations which have been performed in e04gd.
nf
Type: System..::..Int32%
On exit: the number of times that the residuals have been evaluated (i.e., number of calls of lsqfun with iflag set to 2).
ifail
Type: System..::..Int32%
On exit: ifail=0 unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

Description

e04gd is essentially identical to the method LSQFDN in the NPL Algorithms Library. It is applicable to problems of the form
MinimizeFx=i=1mfix2
where x=x1,x2,,xnT and mn. (The functions fix are often referred to as ‘residuals’.)
You must supply a method to calculate the values of the fix and their first derivatives fixj at any point x.
From a starting point x1 supplied by you, the method generates a sequence of points x2,x3,, which is intended to converge to a local minimum of Fx. The sequence of points is given by
xk+1=xk+αkpk
where the vector pk is a direction of search, and αk is chosen such that Fxk+αkpk is approximately a minimum with respect to αk.
The vector pk used depends upon the reduction in the sum of squares obtained during the last iteration. If the sum of squares was sufficiently reduced, then pk is the Gauss–Newton direction; otherwise finite difference estimates of the second derivatives of the fix are taken into account.
The method is designed to ensure that steady progress is made whatever the starting point, and to have the rapid ultimate convergence of Newton's method.

References

Gill P E and Murray W (1978) Algorithms for the solution of the nonlinear least squares problem SIAM J. Numer. Anal. 15 977–992

Error Indicators and Warnings

Note: e04gd may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDFJAC, LDV, IW, LIW, W, LW) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
ifail<0
A negative value of ifail indicates an exit from e04gd because you have set iflag negative in lsqfun. The value of ifail will be the same as your setting of iflag.
ifail=1
On entry,n<1,
orm<n,
ormaxcal<1,
oreta<0.0,
oreta1.0,
orxtol<0.0,
orstepmx<xtol,
When this exit occurs, no values will have been assigned to fsumsq, or to the elements of fvec, fjac, s or v.
ifail=2
There have been maxcal evaluations of the residuals. If steady reductions in the sum of squares, Fx, were monitored up to the point where this exit occurred, then the exit probably occurred simply because maxcal was set too small, so the calculations should be restarted from the final point held in x. This exit may also indicate that Fx has no minimum.
ifail=3
The conditions for a minimum have not all been satisfied, but a lower point could not be found. This could be because xtol has been set so small that rounding errors in the evaluation of the residuals and derivatives make attainment of the convergence conditions impossible.
ifail=4
The method for computing the singular value decomposition of the Jacobian matrix has failed to converge in a reasonable number of sub-iterations. It may be worth applying e04gd again starting with an initial approximation which is not too close to the point at which the failure occurred.
ifail=-9000
An error occured, see message report.
ifail=-6000
Invalid Parameters value
ifail=-4000
Invalid dimension for array value
ifail=-8000
Negative dimension for array value
ifail=-6000
Invalid Parameters value
The values ifail=23 or 4 may also be caused by mistakes in lsqfun, by the formulation of the problem or by an awkward function. If there are no such mistakes it is worth restarting the calculations from a different starting point (not the point at which the failure occurred) in order to avoid the region which caused the failure.

Accuracy

A successful exit (ifail=0) is made from e04gd when the matrix of approximate second derivatives of Fx is positive definite, and when (B1, B2 and B3) or B4 or B5 hold, where
B1αk×pk<xtol+ε×1.0+xkB2Fk-Fk-1<xtol+ε2×1.0+FkB3gk<ε1/3×1.0+FkB4Fk<ε2B5gk<ε×Fk1/2
and where . and ε are as defined in xtol, and Fk and gk are the values of Fx and its vector of estimated first derivatives at xk.
If ifail=0 then the vector in x on exit, xsol, is almost certainly an estimate of xtrue, the position of the minimum to the accuracy specified by xtol.
If ifail=3, then xsol may still be a good estimate of xtrue, but to verify this you should make the following checks. If
(a) the sequence Fxk converges to Fxsol at a superlinear or a fast linear rate, and
(b) gxsolTgxsol<10ε, where T denotes transpose, then it is almost certain that xsol is a close approximation to the minimum.
When (b) is true, then usually Fxsol is a close approximation to Fxtrue. The values of Fxk can be calculated in lsqmon, and the vector gxsol can be calculated from the contents of fvec and fjac on exit from e04gd.
Further suggestions about confirmation of a computed solution are given in the E04 class.

Parallelism and Performance

None.

Further Comments

The number of iterations required depends on the number of variables, the number of residuals, the behaviour of Fx, the accuracy demanded and the distance of the starting point from the solution. The number of multiplications performed per iteration of e04gd varies, but for mn is approximately n×m2+On3. In addition, each iteration makes at least one call of lsqfun. So, unless the residuals and their derivatives can be evaluated very quickly, the run time will be dominated by the time spent in lsqfun.
Ideally, the problem should be scaled so that, at the solution, Fx and the corresponding values of the xj are each in the range -1,+1, and so that at points one unit away from the solution, Fx differs from its value at the solution by approximately one unit. This will usually imply that the Hessian matrix of Fx at the solution is well-conditioned. It is unlikely that you will be able to follow these recommendations very closely, but it is worth trying (by guesswork), as sensible scaling will reduce the difficulty of the minimization problem, so that e04gd will take less computer time.
When the sum of squares represents the goodness-of-fit of a nonlinear model to observed data, elements of the variance-covariance matrix of the estimated regression coefficients can be computed by a subsequent call to (E04YCF not in this release), using information returned in the arrays s and v. See (E04YCF not in this release) for further details.

Example

See Also