e04he {NAGFWrappers}  R Documentation 
e04he 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 and second 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).
e04he(m, lsqfun, lsqhes, 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 e04he to terminate immediately.)

lsqhes 
function lsqhes must calculate the elements of the symmetric matrix B(x) = ∑_i = 1^mf_i(x)G_i(x), at any point x, where G_i(x) is the Hessian matrix of f_i(x). (As with lsqfun, there is the option of causing e04he 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 This argument is present so as to enable you to limit the number of times that lsqfun is called by e04he. There will be an error exit (see the Errors section in Fortran library documentation) after maxcal calls of lsqfun. 
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 Specifies the frequency with which lsqmon is to be called. iprint > 0: lsqmon is called once every iprint iterations and just before exit from e04he. 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 e04he involves a linear minimization (i.e., minimization of F(x^(k) + α^(k)p^(k)) with respect to α^(k)). eta must lie in the range 0.0 <= eta < 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). 
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.) 
R interface to the NAG Fortran routine E04HEF.
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 e04he. 
NF 
integer The number of times that the residuals and Jacobian matrix have been evaluated (i.e., number of calls of lsqfun). 
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/e04hef.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] 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)) } lsqhes = function(iflag, m, n, fvec, xc, lb) { b < as.matrix(mat.or.vec(lb, 1)) b[1] < 0 b[2] < 0 sum22 < 0 sum32 < 0 sum33 < 0 for (i in c(1:m)) { dummy < 2 %*% t[i, 1]/(xc[2] %*% t[i, 2] + xc[3] %*% t[i, 3])^3 sum22 < sum22 + fvec[i] %*% dummy %*% t[i, 2]^2 sum32 < sum32 + fvec[i] %*% dummy %*% t[i, 2] %*% t[i, 3] sum33 < sum33 + fvec[i] %*% dummy %*% t[i, 3]^2 } b[3] < sum22 b[4] < 0 b[5] < sum32 b[6] < sum33 list(IFLAG = as.integer(iflag), B = as.matrix(b)) } 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) e04he(m, lsqfun, lsqhes, lsqmon, maxcal, xtol, x)