e04gd {NAGFWrappers}  R Documentation 
e04gd is a comprehensive modified GaussNewton algorithm for finding an unconstrained minimum of a sum of squares of m nonlinear functions in n variables (m >= n). First derivatives are required.
The function is intended for functions which have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities).
e04gd(m, lsqfun, lsqmon, maxcal, xtol, x, n = nrow(x), iprint = 1, eta = if (n==1) 0.0 else 0.5, stepmx = 100000.0)
m 
integer 
lsqfun 
function lsqfun must calculate the vector of values f_i(x) and Jacobian matrix of first derivatives ( \partial f_i)/( \partial x_j) 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 argument to cause e04gd to terminate immediately.)

lsqmon 
function If iprint >= 0, you must supply lsqmon which is suitable for monitoring the minimization process. lsqmon must not change the values of any of its arguments.

maxcal 
integer Enables you to limit the number of times that lsqfun is called by e04gd. There will be an error exit (see the Errors section in Fortran library documentation) 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. 
xtol 
double The accuracy in x to which the solution is required. 
x 
double array x[j]must be set to a guess at the jth component of the position of the minimum for j=1 . . . n. 
n 
integer: default = nrow(x) The number m of residuals, f_i(x), and the number n of variables, x_j. 
iprint 
integer: default = 1 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. 
eta 
double: default = if (n==1) 0.0 else 0.5 Every iteration of e04gd involves a linear minimization, i.e., minimization of F(x^(k) + α^(k)p^(k)) 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). 
stepmx 
double: default = 100000.0 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 = 1^n(x_j^(k)  x_j^(k  1))^2 <= (stepmx)^2 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 x^(k) entering a region where the problem is illbehaved and can help avoid overflow in the evaluation of F(x). However, an underestimate of stepmx can lead to inefficiency. 
R interface to the NAG Fortran routine E04GDF.
X 
double array The final point x^(k). Thus, if ifail =0 on exit, x[j] is the jth component of the estimated position of the minimum. 
FSUMSQ 
double The value of F(x), the sum of squares of the residuals f_i(x), at the final point given in x. 
FVEC 
double array The value of the residual f_i(x) at the final point given in x for i=1 . . . m. 
FJAC 
double array The value of the first derivative ( \partial f_i)/( \partial x_j) evaluated at the final point given in x for j=1 . . . n for i=1 . . . m. 
S 
double array 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 
double array The matrix V associated with the singular value decomposition J = USV^T 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 J^TJ. 
NITER 
integer The number of iterations which have been performed in e04gd. 
NF 
integer The number of times that the residuals have been evaluated (i.e., number of calls of lsqfun with iflag set to 2). 
IFAIL 
integer ifail =0unless the function detects an error or a warning has been flagged (see the Errors section in Fortran library documentation). 
NAG
http://www.nag.co.uk/numeric/FL/nagdoc_fl23/pdf/E04/e04gdf.pdf
ifail < 0 lsqfun = function(iflag, m, n, xc, ljc) { fvec < as.matrix(mat.or.vec(m, 1)) fjacc < as.matrix(mat.or.vec(ljc, n)) for (i in c(1:m)) { denom < xc[2] %*% t[i, 2] + xc[3] %*% t[i, 3] if (iflag != 1) { fvec[i] < xc[1] + t[i, 1]/denom  y[i] } if (iflag != 0) { fjacc[i, 1] < 1 dummy < 1/(denom %*% denom) fjacc[i, 2] < t[i, 1] %*% t[i, 2] %*% dummy fjacc[i, 3] < t[i, 1] %*% t[i, 3] %*% dummy } } list(IFLAG = as.integer(iflag), FVEC = as.matrix(fvec), FJAC = as.matrix(fjacc)) } lsqmon = function(m, n, xc, fvec, fjacc, ljc, s, igrade, niter, nf) { list() } m < 15 maxcal < 150 xtol < 1.05418557512311e07 x < matrix(c(0.5, 1, 1.5), nrow = 3, ncol = 1, byrow = TRUE) iw < matrix(c(0), nrow = 1, ncol = 1, byrow = TRUE) w < as.matrix(mat.or.vec(105, 1)) y < matrix(c(0.14, 0.18, 0.22, 0.25, 0.29, 0.32, 0.35, 0.39, 0.37, 0.58, 0.73, 0.96, 1.34, 2.1, 4.39), nrow = 1, ncol = 15, byrow = TRUE) t < matrix(c(1, 15, 1, 2, 14, 2, 3, 13, 3, 4, 12, 4, 5, 11, 5, 6, 10, 6, 7, 9, 7, 8, 8, 8, 9, 7, 7, 10, 6, 6, 11, 5, 5, 12, 4, 4, 13, 3, 3, 14, 2, 2, 15, 1, 1), nrow = 15, ncol = 3, byrow = TRUE) e04gd(m, lsqfun, lsqmon, maxcal, xtol, x)