# modgr: Compute gradient from residuals and Jacobian. In nlmrt: Functions for Nonlinear Least Squares Solutions

## Description

For a nonlinear model originally expressed as an expression of the form lhs ~ formula_for_rhs assume we have a resfn and jacfn that compute the residuals and the Jacobian at a set of parameters. This routine computes the gradient, that is, t(Jacobian) . residuals.

## Usage

 `1` ``` modgr(prm, resfn, jacfn, ...) ```

## Arguments

 `prm` A parameter vector. For our example, we could use start=c(b1=1, b2=2.345, b3=0.123) However, the names are NOT used, only positions in the vector. `resfn` A function to compute the residuals of our model at a parameter vector. `jacfn` A function to compute the Jacobian of the residuals at a paramter vector. `...` Any data needed for computation of the residual vector from the expression rhsexpression - lhsvar. Note that this is the negative of the usual residual, but the sum of squares is the same.

## Details

`modgr` calls resfn to compute residuals and jacfn to compute the Jacobian at the parameters `prm` using external data in the dot arguments. It then computes the gradient using t(Jacobian) . residuals.

Note that it appears awkward to use this function in calls to optimization routines. The author would like to learn why.

## Value

The numeric vector with the gradient of the sum of squares at the paramters.

## Note

Special notes, if any, will appear here.

## Author(s)

John C Nash <nashjc@uottawa.ca>

## References

Nash, J. C. (1979, 1990) _Compact Numerical Methods for Computers. Linear Algebra and Function Minimisation._ Adam Hilger./Institute of Physics Publications

Function `nls()`, packages `optim` and `optimx`.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ``` cat("See examples in nlmrt-package.Rd\n") y <- c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, 38.558, 50.156, 62.948, 75.995, 91.972) # for testing tt <- seq_along(y) # for testing f <- y ~ b1/(1 + b2 * exp(-1 * b3 * tt)) p <- c(b1 = 1, b2 = 1, b3 = 1) myres <- model2resfun(f, p) myjac <- model2jacfun(f, p) mygr <- model2grfun(f, p) gr <- mygr(p, tt = tt, y = y) grm <- modgr(p, myres, myjac, tt = tt, y = y) cat("max(abs(grm - gr)) =",max(abs(grm-gr)),"\n") ```