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


public static void e04fc(
	int m,
	int n,
	E04..::..E04FC_LSQFUN lsqfun,
	E04..::..E04FC_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 e04fc ( _
	m As Integer, _
	n As Integer, _
	lsqfun As E04..::..E04FC_LSQFUN, _
	lsqmon As E04..::..E04FC_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++
static void e04fc(
	int m, 
	int n, 
	E04..::..E04FC_LSQFUN^ lsqfun, 
	E04..::..E04FC_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
static member e04fc : 
        m : int * 
        n : int * 
        lsqfun : E04..::..E04FC_LSQFUN * 
        lsqmon : E04..::..E04FC_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 


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

A delegate of type E04FC_LSQFUN.

Note:  lsqfun should be tested separately before being used in conjunction with e04fc.
Type: NagLibrary..::..E04..::..E04FC_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 E04FC_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 helpful to print xc, the estimated gradient of the sum of squares, niter and nf.
Type: System..::..Int32
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 e04fc.
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.
Type: System..::..Int32
On entry: the limit you set on the number of times that lsqfun may be called by e04fc. There will be an error exit (see [Error Indicators and Warnings]) after maxcal calls of lsqfun.
Suggested value: maxcal=400×n.
Constraint: maxcal1.
Type: System..::..Double
Every iteration of e04fc involves a linear minimization, i.e., minimization of Fxk+αkpk 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 e04fc, 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.
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
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].)
Suggested value: 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ε, e04fc will use 10ε instead of xtol, since 10ε is probably the smallest reasonable setting.
Constraint: xtol0.0.
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.) e04fc will ensure that, for each iteration,
where k is the iteration number. Thus, if the problem has more than one solution, e04fc is most likely to find the one nearest to the starting point. On difficult problems, a realistic choice can prevent the sequence 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.
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.
Type: System..::..Double%
On exit: the value of Fx, the sum of squares of the residuals fix, at the final point given in x.
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.
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, n]
Note: dim1 must satisfy the constraint: dim1m
On exit: the estimate of the first derivative fixj at the final point given in x, for i=1,2,,m and j=1,2,,n.
Type: array<System..::..Double>[]()[][]
An array of size [n]
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.
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
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.
Type: System..::..Int32%
On exit: the number of iterations which have been performed in e04fc.
Type: System..::..Int32%
On exit: the number of times that the residuals have been evaluated (i.e., number of calls of lsqfun).
Type: System..::..Int32%
On exit: ifail=0 unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).


e04fc is essentially identical to the method LSQNDN in the NPL Algorithms Library. It is applicable to problems of the form
where x=x1,x2,,xnT and mn. (The functions fix are often referred to as ‘residuals’.)
You must supply lsqfun to calculate the values of the fix 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
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 an approximation to the Gauss–Newton direction; otherwise additional function evaluations are made so as to enable pk 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.


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: e04fc 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.
A negative value of ifail indicates an exit from e04fc because you have set iflag negative in lsqfun. The value of ifail will be the same as your setting of iflag.
On entry,n<1,
When this exit occurs, no values will have been assigned to fsumsq, or to the elements of fvec, fjac, s or v.
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.
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 make attainment of the convergence conditions impossible.
The method for computing the singular value decomposition of the estimated Jacobian matrix has failed to converge in a reasonable number of sub-iterations. It may be worth applying e04fc again starting with an initial approximation which is not too close to the point at which the failure occurred.
An error occured, see message report.
Invalid Parameters value
Invalid dimension for array value
Negative dimension for array value
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.


A successful exit (ifail=0) is made from e04fc when (B1, B2 and B3) or B4 or B5 hold, where
and where . and ε are as defined in [Parameters], 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 e04fc.
Further suggestions about confirmation of a computed solution are given in the E04 class.

Parallelism and Performance


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 e04fc varies, but for mn is approximately n×m2+On3. 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, 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 e04fc 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.


This example finds least squares estimates of x1,x2 and x3 in the model
using the 15 sets of data given in the following table.
The program uses 0.5,1.0,1.5 as the initial guess at the position of the minimum.

Example program (C#): e04fce.cs

Example program data: e04fce.d

Example program results: e04fce.r

See Also