E04HEF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

E04HEF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

E04HEF 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 and second derivatives are required.
The routine is intended for functions which have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities).

2  Specification

SUBROUTINE E04HEF ( M, N, LSQFUN, LSQHES, LSQMON, IPRINT, MAXCAL, ETA, XTOL, STEPMX, X, FSUMSQ, FVEC, FJAC, LDFJAC, S, V, LDV, NITER, NF, IW, LIW, W, LW, IFAIL)
INTEGER  M, N, IPRINT, MAXCAL, LDFJAC, LDV, NITER, NF, IW(LIW), LIW, LW, IFAIL
REAL (KIND=nag_wp)  ETA, XTOL, STEPMX, X(N), FSUMSQ, FVEC(M), FJAC(LDFJAC,N), S(N), V(LDV,N), W(LW)
EXTERNAL  LSQFUN, LSQHES, LSQMON

3  Description

E04HEF is essentially identical to the subroutine LSQSDN 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 subroutines to calculate the values of the fix and their first derivatives and second derivatives at any point x.
From a starting point x 1  supplied by you, the routine generates a sequence of points x 2 ,x 3 ,, which is intended to converge to a local minimum of Fx. The sequence of points is given by
x k+1 =x k +αkp k  
where the vector p k  is a direction of search, and αk is chosen such that Fx k +αkp k  is approximately a minimum with respect to α k .
The vector p k  used depends upon the reduction in the sum of squares obtained during the last iteration. If the sum of squares was sufficiently reduced, then p k  is the Gauss–Newton direction; otherwise 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.

4  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

5  Parameters

1:     M – INTEGERInput
2:     N – INTEGERInput
On entry: the number m of residuals, fix, and the number n of variables, xj.
Constraint: 1NM.
3:     LSQFUN – SUBROUTINE, supplied by the user.External Procedure
LSQFUN must calculate the vector of values fix and Jacobian matrix of first derivatives fi xj  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 E04HEF to terminate immediately.)
The specification of LSQFUN is:
SUBROUTINE LSQFUN ( IFLAG, M, N, XC, FVEC, FJAC, LDFJAC, IW, LIW, W, LW)
INTEGER  IFLAG, M, N, LDFJAC, IW(LIW), LIW, LW
REAL (KIND=nag_wp)  XC(N), FVEC(M), FJAC(LDFJAC,N), W(LW)
Important: the dimension declaration FJAC must contain the variable LDFJAC, not an integer constant.
1:     IFLAG – INTEGERInput/Output
On entry: to LSQFUN, IFLAG will be set to 2.
On exit: if it is not possible to evaluate the fix or their first derivatives at the point given in XC (or if it wished to stop the calculations for any other reason), you should reset IFLAG to some negative number and return control to E04HEF. E04HEF will then terminate immediately, with IFAIL set to your setting of IFLAG.
2:     M – INTEGERInput
On entry: m, the numbers of residuals.
3:     N – INTEGERInput
On entry: n, the numbers of variables.
4:     XCN – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which the values of the fi and the fi xj  are required.
5:     FVECM – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset to a negative number, FVECi must contain the value of fi at the point x, for i=1,2,,m.
6:     FJACLDFJACN – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset to a negative number, FJACij must contain the value of fi xj  at the point x, for i=1,2,,m and j=1,2,,n.
7:     LDFJAC – INTEGERInput
On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04HEF is called.
8:     IWLIW – INTEGER arrayWorkspace
9:     LIW – INTEGERInput
10:   WLW – REAL (KIND=nag_wp) arrayWorkspace
11:   LW – INTEGERInput
LSQFUN is called with E04HEF's parameters IW, LIW, W, LW as these parameters. They are present so that, when other library routines require the solution of a minimization subproblem, constants needed for the evaluation of residuals can be passed through IW and W. Similarly, you could pass quantities of LSQFUN from the segment which calls E04HEF by using partitions of IW and W beyond those used as workspace by E04HEF. However, because of the danger of mistakes in partitioning, it is recommended that you should pass information to LSQFUN via COMMON global variables and not use IW or W at all. In any case you must not change the elements of IW and W used as workspace by E04HEF.
LSQFUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04HEF is called. Parameters denoted as Input must not be changed by this procedure.
Note:  LSQFUN should be tested separately before being used in conjunction with E04HEF.
4:     LSQHES – SUBROUTINE, supplied by the user.External Procedure
LSQHES must calculate the elements of the symmetric matrix
Bx=i=1mfixGix,  
at any point x, where Gix is the Hessian matrix of fix. (As with LSQFUN, there is the option of causing E04HEF to terminate immediately.)
The specification of LSQHES is:
SUBROUTINE LSQHES ( IFLAG, M, N, FVEC, XC, B, LB, IW, LIW, W, LW)
INTEGER  IFLAG, M, N, LB, IW(LIW), LIW, LW
REAL (KIND=nag_wp)  FVEC(M), XC(N), B(LB), W(LW)
1:     IFLAG – INTEGERInput/Output
On entry: is set to a non-negative number.
On exit: if LSQHES resets IFLAG to some negative number, E04HEF will terminate immediately, with IFAIL set to your setting of IFLAG.
2:     M – INTEGERInput
On entry: m, the numbers of residuals.
3:     N – INTEGERInput
On entry: n, the numbers of variables.
4:     FVECM – REAL (KIND=nag_wp) arrayInput
On entry: the value of the residual fi at the point x, for i=1,2,,m, so that the values of the fi can be used in the calculation of the elements of B.
5:     XCN – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which the elements of B are to be evaluated.
6:     BLB – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset to a negative number, B must contain the lower triangle of the matrix Bx, evaluated at the point x, stored by rows. (The upper triangle is not required because the matrix is symmetric.) More precisely, Bjj-1/2+k must contain i=1mfi 2fi xjxk  evaluated at the point x, for j=1,2,,n and k=1,2,,j.
7:     LB – INTEGERInput
On entry: the length of the array B.
8:     IWLIW – INTEGER arrayWorkspace
9:     LIW – INTEGERInput
10:   WLW – REAL (KIND=nag_wp) arrayWorkspace
11:   LW – INTEGERInput
As in LSQFUN, these parameters correspond to the parameters IW, LIW, W, LW of E04HEF. LSQHES must not change the sections of IW and W required as workspace by E04HEF. Again, it is recommended that you should pass quantities to LSQHES via COMMON global variables and not use IW or W at all.
LSQHES must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04HEF is called. Parameters denoted as Input must not be changed by this procedure.
Note:  LSQHES should be tested separately before being used in conjunction with E04HEF.
5:     LSQMON – SUBROUTINE, supplied by the NAG Library or the user.External Procedure
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 routine E04FDZ can be used as LSQMON.
The specification of LSQMON is:
SUBROUTINE LSQMON ( M, N, XC, FVEC, FJAC, LDFJAC, S, IGRADE, NITER, NF, IW, LIW, W, LW)
INTEGER  M, N, LDFJAC, IGRADE, NITER, NF, IW(LIW), LIW, LW
REAL (KIND=nag_wp)  XC(N), FVEC(M), FJAC(LDFJAC,N), S(N), W(LW)
Important: the dimension declaration for FJAC must contain the variable LDFJAC, not an integer constant.
1:     M – INTEGERInput
On entry: m, the numbers of residuals.
2:     N – INTEGERInput
On entry: n, the numbers of variables.
3:     XCN – REAL (KIND=nag_wp) arrayInput
On entry: the coordinates of the current point x.
4:     FVECM – REAL (KIND=nag_wp) arrayInput
On entry: the values of the residuals fi at the current point x.
5:     FJACLDFJACN – REAL (KIND=nag_wp) arrayInput
On entry: FJACij contains the value of fi xj  at the current point x, for i=1,2,,m and j=1,2,,n.
6:     LDFJAC – INTEGERInput
On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04HEF is called.
7:     SN – REAL (KIND=nag_wp) arrayInput
On entry: the singular values of the current Jacobian matrix. Thus S may be useful as information about the structure of your problem. (If IPRINT>0, LSQMON is called at the initial point before the singular values have been calculated, so the elements of S are set to zero for the first call of LSQMON.)
8:     IGRADE – INTEGERInput
On entry: E04HEF estimates the dimension of the subspace for which the Jacobian matrix can be used as a valid approximation to the curvature (see Gill and Murray (1978)). This estimate is called the grade of the Jacobian matrix, and IGRADE gives its current value.
9:     NITER – INTEGERInput
On entry: the number of iterations which have been performed in E04HEF.
10:   NF – INTEGERInput
On entry: the number of times that LSQFUN has been called so far. Thus NF gives the number of evaluations of the residuals and the Jacobian matrix.
11:   IWLIW – INTEGER arrayWorkspace
12:   LIW – INTEGERInput
13:   WLW – REAL (KIND=nag_wp) arrayWorkspace
14:   LW – INTEGERInput
As in LSQFUN and LSQHES, these parameters correspond to the parameters IW, LIW, W, LW of E04HEF. They are included in LSQMON's parameter list primarily for when E04HEF is called by other library routines.
LSQMON must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04HEF is called. Parameters denoted as Input must not be changed by this procedure.
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 Section 7. It is usually helpful to also print XC, the gradient of the sum of squares, NITER and NF.
6:     IPRINT – INTEGERInput
On entry: specifies the frequency with which LSQMON is to be called.
IPRINT>0
LSQMON is called once every IPRINT iterations and just before exit from E04HEF.
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.
7:     MAXCAL – INTEGERInput
On entry: this parameter is present so as to enable you to limit the number of times that LSQFUN is called by E04HEF. There will be an error exit (see Section 6) after MAXCAL calls of LSQFUN.
Suggested value: MAXCAL=50×n.
Constraint: MAXCAL1.
8:     ETA – REAL (KIND=nag_wp)Input
On entry: every iteration of E04HEF involves a linear minimization (i.e., minimization of Fx k +α k p k  with respect to α k ). ETA must lie in the range 0.0ETA<1.0, and 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 performed by E04HEF, they will increase the number of calls of LSQFUN made each iteration. On balance it is usually more efficient to perform a low accuracy minimization.
Suggested value: ETA=0.5 (ETA=0.0 if N=1).
Constraint: 0.0ETA<1.0.
9:     XTOL – REAL (KIND=nag_wp)Input
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 Section 7.)
If Fx and the variables are scaled roughly as described in Section 9 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ε, E04HEF will use 10ε instead of XTOL, since 10ε is probably the smallest reasonable setting.
Constraint: XTOL0.0.
10:   STEPMX – REAL (KIND=nag_wp)Input
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.)
E04HEF will ensure that, for each iteration
j=1nxj k -xj k-1 2 STEPMX 2,  
where k is the iteration number. Thus, if the problem has more than one solution, E04HEF is most likely to find the one nearest to the starting point. On difficult problems, a realistic choice can prevent the sequence of x k  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.
11:   XN – REAL (KIND=nag_wp) arrayInput/Output
On entry: Xj 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 x k . Thus, if IFAIL=0 on exit, Xj is the jth component of the estimated position of the minimum.
12:   FSUMSQ – REAL (KIND=nag_wp)Output
On exit: the value of Fx, the sum of squares of the residuals fix, at the final point given in X.
13:   FVECM – REAL (KIND=nag_wp) arrayOutput
On exit: the value of the residual fix at the final point given in X, for i=1,2,,m.
14:   FJACLDFJACN – REAL (KIND=nag_wp) arrayOutput
On exit: the value of the first derivative fi xj  evaluated at the final point given in X, for i=1,2,,m and j=1,2,,n.
15:   LDFJAC – INTEGERInput
On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04HEF is called.
Constraint: LDFJACM.
16:   SN – REAL (KIND=nag_wp) arrayOutput
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.
17:   VLDVN – REAL (KIND=nag_wp) arrayOutput
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.
18:   LDV – INTEGERInput
On entry: the first dimension of the array V as declared in the (sub)program from which E04HEF is called.
Constraint: LDVN.
19:   NITER – INTEGEROutput
On exit: the number of iterations which have been performed in E04HEF.
20:   NF – INTEGEROutput
On exit: the number of times that the residuals and Jacobian matrix have been evaluated (i.e., number of calls of LSQFUN).
21:   IWLIW – INTEGER arrayCommunication Array
22:   LIW – INTEGERInput
On entry: the dimension of the array IW as declared in the (sub)program from which E04HEF is called.
Constraint: LIW1.
23:   WLW – REAL (KIND=nag_wp) arrayCommunication Array
24:   LW – INTEGERInput
On entry: the dimension of the array W as declared in the (sub)program from which E04HEF is called.
Constraints:
  • if N>1, LW7×N+2×M×N+M+N×N;
  • if N=1, LW9+3×M.
25:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: E04HEF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
IFAIL<0
A negative value of IFAIL indicates an exit from E04HEF because you have set IFLAG negative in LSQFUN or LSQHES. 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,
orLDFJAC<M,
orLDV<N,
orLIW<1,
orLW<7×N+M×N+2×M+N×N when N>1,
orLW<9+3×M when N=1.
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 calls of LSQFUN. 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 E04HEF again starting with an initial approximation which is not too close to the point at which the failure occurred.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.
The values IFAIL=2, 3 and 4 may also be caused by mistakes in LSQFUN or LSQHES, 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.

7  Accuracy

A successful exit (IFAIL=0) is made from E04HEF when the matrix of second derivatives of Fx is positive definite, and when (B1, B2 and B3) or B4 or B5 hold, where
B1 α k ×p k <XTOL+ε×1.0+x k B2 F k -F k-1 < XTOL+ε 2×1.0+F k B3 g k <ε1/3×1.0+F k B4 F k <ε2 B5 g k <ε×F k 1/2  
and where . and ε are as defined in Section 5, and F k  and g k  are the values of Fx and its vector of first derivatives at x k .
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 Fx k  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 Fx k  can be calculated in LSQMON, and the vector gxsol can be calculated from the contents of FVEC and FJAC on exit from E04HEF.
Further suggestions about confirmation of a computed solution are given in the E04 Chapter Introduction.

8  Parallelism and Performance

E04HEF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
E04HEF makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9  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 E04HEF varies, but for mn is approximately n×m2+On3. In addition, each iteration makes at least one call of LSQFUN and some iterations may call LSQHES. So, unless the residuals and their derivatives can be evaluated very quickly, the run time will be dominated by the time spent in LSQFUN (and, to a lesser extent, in LSQHES).
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 E04HEF 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, using information returned in the arrays S and V. See E04YCF for further details.

10  Example

This example finds least squares estimates of x1,x2 and x3 in the model
y=x1+t1x2t2+x3t3  
using the 15 sets of data given in the following table.
y t1 t2 t3 0.14 1.0 15.0 1.0 0.18 2.0 14.0 2.0 0.22 3.0 13.0 3.0 0.25 4.0 12.0 4.0 0.29 5.0 11.0 5.0 0.32 6.0 10.0 6.0 0.35 7.0 9.0 7.0 0.39 8.0 8.0 8.0 0.37 9.0 7.0 7.0 0.58 10.0 6.0 6.0 0.73 11.0 5.0 5.0 0.96 12.0 4.0 4.0 1.34 13.0 3.0 3.0 2.10 14.0 2.0 2.0 4.39 15.0 1.0 1.0  
Before calling E04HEF, the program calls E04YAF and E04YBF to check LSQFUN and LSQHES. It uses 0.5,1.0,1.5 as the initial guess at the position of the minimum.

10.1  Program Text

Program Text (e04hefe.f90)

10.2  Program Data

Program Data (e04hefe.d)

10.3  Program Results

Program Results (e04hefe.r)


E04HEF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015