NAG FL Interface
e04fcf (lsq_​uncon_​mod_​func_​comp)

Settings help

FL Name Style:


FL Specification Language:


1 Purpose

e04fcf is a comprehensive algorithm for finding an unconstrained minimum of a sum of squares of m nonlinear functions in n variables (mn). No 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

Fortran Interface
Subroutine e04fcf ( m, n, lsqfun, lsqmon, iprint, maxcal, eta, xtol, stepmx, x, fsumsq, fvec, fjac, ldfjac, s, v, ldv, niter, nf, iw, liw, w, lw, ifail)
Integer, Intent (In) :: m, n, iprint, maxcal, ldfjac, ldv, liw, lw
Integer, Intent (Inout) :: iw(liw), ifail
Integer, Intent (Out) :: niter, nf
Real (Kind=nag_wp), Intent (In) :: eta, xtol, stepmx
Real (Kind=nag_wp), Intent (Inout) :: x(n), fjac(ldfjac,n), v(ldv,n), w(lw)
Real (Kind=nag_wp), Intent (Out) :: fsumsq, fvec(m), s(n)
External :: lsqfun, lsqmon
C Header Interface
#include <nag.h>
void  e04fcf_ (const Integer *m, const Integer *n,
void (NAG_CALL *lsqfun)(Integer *iflag, const Integer *m, const Integer *n, const double xc[], double fvec[], Integer iw[], const Integer *liw, double w[], const Integer *lw),
void (NAG_CALL *lsqmon)(const Integer *m, const Integer *n, const double xc[], const double fvec[], const double fjac[], const Integer *ldfjac, const double s[], const Integer *igrade, const Integer *niter, const Integer *nf, Integer iw[], const Integer *liw, double w[], const Integer *lw),
const Integer *iprint, const Integer *maxcal, const double *eta, const double *xtol, const double *stepmx, double x[], double *fsumsq, double fvec[], double fjac[], const Integer *ldfjac, double s[], double v[], const Integer *ldv, Integer *niter, Integer *nf, Integer iw[], const Integer *liw, double w[], const Integer *lw, Integer *ifail)
The routine may be called by the names e04fcf or nagf_opt_lsq_uncon_mod_func_comp.

3 Description

e04fcf is essentially identical to the subroutine LSQNDN in the NPL Algorithms Library. It is applicable to problems of the form
MinimizeF(x)=i=1m[fi(x)]2  
where x = (x1,x2,,xn) T and mn. (The functions fi(x) are often referred to as ‘residuals’.)
You must supply lsqfun to calculate the values of the fi(x) 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 F(x). The sequence of points is given by
x (k+1) =x (k) +α(k)p (k)  
where the vector p (k) is a direction of search, and α (k) is chosen such that F(x (k) +α (k) p (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 an approximation to the Gauss–Newton direction; otherwise additional function evaluations are made so as to enable p (k) to be a more accurate approximation to the Newton direction.
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 Arguments

1: m Integer Input
2: n Integer Input
On entry: the number m of residuals, fi(x), 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 fi(x) at any point x. (However, if you do not wish to calculate the residuals at a particular x, there is the option of setting an argument to cause e04fcf to terminate immediately.)
The specification of lsqfun is:
Fortran Interface
Subroutine lsqfun ( iflag, m, n, xc, fvec, iw, liw, w, lw)
Integer, Intent (In) :: m, n, liw, lw
Integer, Intent (Inout) :: iflag, iw(liw)
Real (Kind=nag_wp), Intent (In) :: xc(n)
Real (Kind=nag_wp), Intent (Inout) :: w(lw)
Real (Kind=nag_wp), Intent (Out) :: fvec(m)
C Header Interface
void  lsqfun (Integer *iflag, const Integer *m, const Integer *n, const double xc[], double fvec[], Integer iw[], const Integer *liw, double w[], const Integer *lw)
1: iflag Integer Input/Output
On entry: has a non-negative value.
On exit: if lsqfun resets iflag to some negative number, e04fcf will terminate immediately, with ifail set to your setting of iflag.
2: m Integer Input
On entry: m, the numbers of residuals.
3: n Integer Input
On entry: n, the numbers of variables.
4: xc(n) Real (Kind=nag_wp) array Input
On entry: the point x at which the values of the fi are required.
5: fvec(m) Real (Kind=nag_wp) array Output
On exit: unless iflag is reset to a negative number, fvec(i) must contain the value of fi at the point x, for i=1,2,,m.
6: iw(liw) Integer array Workspace
7: liw Integer Input
8: w(lw) Real (Kind=nag_wp) array Workspace
9: lw Integer Input
lsqfun is called with these arguments as in the call to e04fcf, so you can pass quantities to lsqfun from the subroutine which calls e04fcf by using partitions of iw and w beyond those used as workspace by e04fcf. However, because of the danger of mistakes in partitioning, it is recommended that this facility be used very selectively, e.g., for stable applications packages which need to pass their own variable dimension workspace to lsqfun. It is recommended that the normal method for passing information from your subroutine to lsqfun should be via COMMON global variables. In any case, you must not change liw, lw or the elements of iw and w used as workspace by e04fcf.
lsqfun must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which e04fcf is called. Arguments denoted as Input must not be changed by this procedure.
Note: lsqfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by e04fcf. If your code inadvertently does return any NaNs or infinities, e04fcf is likely to produce unexpected results.
lsqfun should be tested separately before being used in conjunction with e04fcf.
4: 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 arguments.
If iprint<0, the dummy routine e04fdz can be used as lsqmon.
The specification of lsqmon is:
Fortran Interface
Subroutine lsqmon ( m, n, xc, fvec, fjac, ldfjac, s, igrade, niter, nf, iw, liw, w, lw)
Integer, Intent (In) :: m, n, ldfjac, igrade, niter, nf, liw, lw
Integer, Intent (Inout) :: iw(liw)
Real (Kind=nag_wp), Intent (In) :: xc(n), fvec(m), fjac(ldfjac,n), s(n)
Real (Kind=nag_wp), Intent (Inout) :: w(lw)
C Header Interface
void  lsqmon (const Integer *m, const Integer *n, const double xc[], const double fvec[], const double fjac[], const Integer *ldfjac, const double s[], const Integer *igrade, const Integer *niter, const Integer *nf, Integer iw[], const Integer *liw, double w[], const Integer *lw)
Important: the dimension declaration for fjac must contain the variable ldfjac, not an integer constant.
1: m Integer Input
On entry: m, the numbers of residuals.
2: n Integer Input
On entry: n, the numbers of variables.
3: xc(n) Real (Kind=nag_wp) array Input
On entry: the coordinates of the current point x.
4: fvec(m) Real (Kind=nag_wp) array Input
On entry: the values of the residuals fi at the current point x.
5: fjac(ldfjac,n) Real (Kind=nag_wp) array Input
On entry: fjac(i,j) contains the value of fi xj at the current point x, for i=1,2,,m and j=1,2,,n.
6: ldfjac Integer Input
On entry: the first dimension of the array fjac as declared in the (sub)program from which e04fcf is called.
7: s(n) Real (Kind=nag_wp) array Input
On entry: the singular values of the current approximation to the Jacobian matrix. Thus s may be useful as information about the structure of your problem.
8: igrade Integer Input
On entry: e04fcf 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 Integer Input
On entry: the number of iterations which have been performed in e04fcf.
10: nf Integer Input
On entry: the number of times that lsqfun has been called so far. (However, for intermediate calls of lsqmon, nf is calculated on the assumption that the latest linear search has been successful. If this is not the case, the n evaluations allowed for approximating the Jacobian at the new point will not in fact have been made. nf will be accurate at the final call of lsqmon.)
11: iw(liw) Integer array Workspace
12: liw Integer Input
13: w(lw) Real (Kind=nag_wp) array Workspace
14: lw Integer Input
These arguments correspond to the arguments iw, liw, w and lw of e04fcf. They are included in lsqmon's argument list primarily for when e04fcf 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 e04fcf is called. Arguments 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 F(x) mentioned in Section 7. It is usually helpful to print xc, the estimated gradient of the sum of squares, niter and nf.
5: iprint Integer Input
On entry: the frequency with which lsqmon is to be called.
If iprint>0, lsqmon is called once every iprint iterations and just before exit from e04fcf.
If iprint=0, lsqmon is just called at the final point.
If iprint<0, lsqmon is not called at all.
iprint should normally be set to a small positive number.
Suggested value: iprint=1.
6: maxcal Integer Input
On entry: the limit you set on the number of times that lsqfun may be called by e04fcf. There will be an error exit (see Section 6) after maxcal calls of lsqfun.
Suggested value: maxcal=400×n.
Constraint: maxcal1.
7: eta Real (Kind=nag_wp) Input
Every iteration of e04fcf involves a linear minimization, i.e., minimization of F(x (k) +α (k) p (k) ) with respect to α (k) .
On entry: specifies how accurately the 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 e04fcf, 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.
8: 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.)
Suggested value: if F(x) 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ε, e04fcf will use 10ε instead of xtol, since 10ε is probably the smallest reasonable setting.
Constraint: xtol0.0.
9: 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.) e04fcf will ensure that, for each iteration,
j=1n(xj (k) -xj (k-1) )2 (stepmx) 2, 
where k is the iteration number. Thus, if the problem has more than one solution, e04fcf is most likely to find the one nearest to the starting point. On difficult problems, a realistic choice can prevent the sequence x (k) entering a region where the problem is ill-behaved and can help avoid overflow in the evaluation of F(x). However, an underestimate of stepmx can lead to inefficiency.
Suggested value: stepmx=100000.0.
Constraint: stepmxxtol.
10: x(n) Real (Kind=nag_wp) array Input/Output
On entry: x(j) 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, x(j) is the jth component of the estimated position of the minimum.
11: fsumsq Real (Kind=nag_wp) Output
On exit: the value of F(x), the sum of squares of the residuals fi(x), at the final point given in x.
12: fvec(m) Real (Kind=nag_wp) array Output
On exit: the value of the residual fi(x) at the final point given in x, for i=1,2,,m.
13: fjac(ldfjac,n) Real (Kind=nag_wp) array Output
On exit: the estimate of the first derivative fi xj at the final point given in x, for i=1,2,,m and j=1,2,,n.
14: ldfjac Integer Input
On entry: the first dimension of the array fjac as declared in the (sub)program from which e04fcf is called.
Constraint: ldfjacm.
15: s(n) Real (Kind=nag_wp) array Output
On exit: the singular values of the estimated Jacobian matrix at the final point. Thus s may be useful as information about the structure of your problem.
16: v(ldv,n) Real (Kind=nag_wp) array Output
On exit: the matrix V associated with the singular value decomposition
J=USVT  
of the estimated 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.
17: ldv Integer Input
On entry: the first dimension of the array v as declared in the (sub)program from which e04fcf is called.
Constraint: ldvn.
18: niter Integer Output
On exit: the number of iterations which have been performed in e04fcf.
19: nf Integer Output
On exit: the number of times that the residuals have been evaluated (i.e., number of calls of lsqfun).
20: iw(liw) Integer array Workspace
21: liw Integer Input
On entry: the dimension of the array iw as declared in the (sub)program from which e04fcf is called.
Constraint: liw1.
22: w(lw) Real (Kind=nag_wp) array Workspace
23: lw Integer Input
On entry: the dimension of the array w as declared in the (sub)program from which e04fcf is called.
Constraints:
  • if n>1, lw6×n+m×n+2×m+n×(n-1)/2;
  • if n=1, lw7+3×m.
24: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of -1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value -1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value -1 is recommended since useful values can be provided in some output arguments even when ifail0 on exit. 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).
Errors or warnings detected by the routine:
Note: in some cases e04fcf may return useful information.
ifail=1
On entry, eta=value.
Constraint: 0.0eta<1.0.
On entry, ldfjac=value and m=value.
Constraint: ldfjacm.
On entry, ldv=value and n=value.
Constraint: ldvn.
On entry, liw=value.
Constraint: liw1.
On entry, m=value and n=value.
Constraint: mn.
On entry, maxcal=value.
Constraint: maxcal1.
On entry, n=value.
Constraint: n1.
On entry, n=1 and lw=value.
Constraint: if n=1 then lw7+3×m; that is, value.
On entry, n>1 and lw=value.
Constraint: if n>1 then lw6×n+m×n+2×m+n×(n-1)/2; that is, value.
On entry, stepmx=value and xtol=value.
Constraint: stepmxxtol.
On entry, xtol=value.
Constraint: xtol0.0.
ifail=2
There have been maxcal=value calls to lsqfun.
If steady reductions in the sum of squares, F(x), 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 F(x) has no minimum.
The error 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.
ifail=3
The conditions for a minimum have not all been satisfied, but a lower point could not be found. See Section 7 for further information.
The error 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.
ifail=4
Failure in computing SVD of estimated Jacobian matrix.
It may be worth applying e04fcf again starting with an initial approximation which is not too close to the point at which the failure occurred.
The error 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.
ifail<0
User requested termination by setting iflag negative in lsqfun.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

A successful exit (ifail=0) is made from e04fcf 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+xtol)×(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 F(x) and its vector of estimated 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
  1. (a)the sequence {F(x (k) )} converges to F(xsol) at a superlinear or a fast linear rate, and
  2. (b) g(xsol)T g(xsol) < 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 F(xsol) is a close approximation to F(xtrue). The values of F(x (k) ) can be calculated in lsqmon, and the vector g(xsol) can be calculated from the contents of fvec and fjac on exit from e04fcf.
Further suggestions about confirmation of a computed solution are given in the E04 Chapter Introduction.

8 Parallelism and Performance

e04fcf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
e04fcf 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 F(x), the accuracy demanded and the distance of the starting point from the solution. The number of multiplications performed per iteration of e04fcf varies, but for mn is approximately n×m2+O(n3). In addition, each iteration makes at least n+1 calls of lsqfun. So, unless the residuals 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, F(x) 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, F(x) differs from its value at the solution by approximately one unit. This will usually imply that the Hessian matrix of F(x) 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 e04fcf 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  
The program uses (0.5,1.0,1.5) as the initial guess at the position of the minimum.

10.1 Program Text

Program Text (e04fcfe.f90)

10.2 Program Data

Program Data (e04fcfe.d)

10.3 Program Results

Program Results (e04fcfe.r)