Contents Previous Next Subchapters Current Chapters-> interp interp1 lagrange polyfit smospl cubespl cubeval interp2 mlmode_interp2 snewton brent linlsq linlsqb nlsq dnlsq nlsqbox dnlsqb conjdir neldermead conjgrad minline lemke Qpos Qbox Qpro sqp relative fordiff cendiff autodiff testder testgrad testhess Parent Chapters-> Omatrix6 fit dnlsq Search Tools-> contents reference index search

Nonlinear Least Squares With Derivatives
 Syntax `dnlsq(function `fval`, `xini`, `scale`, `eps`, `maxit`, `level`)` See Also nlsq , dnlsqb

Description
Uses the Gauss-Newton method to solve the problem ```                      2      minimize |f(x)|   with respect to x. ```If analytic derivatives of `f(x)` are not available, the function nlsq can be used to solve this problem.

xini
The real or double-precision column vector xini specifies the point at which to start the Gauss-Newton method.

scale
The vector scale has the same type and dimension as xini, and `scale(i)` is the maximum absolute change in `x(i)` per iteration. Every element of scale must be greater than 0.

eps
The scalar eps has the same type as xini. Convergence is accepted if the maximum absolute change in `x(i)` is less than eps times `scale(i)` for all `i`.

maxit
The scalar integer maxit specifies the maximum number of iterations to try before giving up on convergence.

level
The scalar integer level specifies the level of tracing inside `dnlsq`. If `level > 1`, the value of `|f(x)|^2` is printed at each iteration.

fval

fval(x, fout)
In the call fval`(`x`, `fout`)` the vector x has the same type and dimension as xini and specifies the point at which to evaluate `f(x)`. The input value of fout has no effect, and its output value is the value of `f(x)` as a column vector with the same type as x.

fval(x, fout, dfout)
In the call fval`(`x`, `fout`, `dfout`)` the arguments x and fout have the same meaning as above. The input value of dfout has no effect and its output value is the derivative of `f` with respect to `x` and has the same type and row dimension as fout and a column dimension equal to the row dimension of x. The (i,j)-th element of dfout is the derivative of the i-th component of `f(x)` with respect to the j-th component of `x`.

Return Value
The (i+1)-th column of the return value is the value of `x` at the i-th iteration. (The first column contains the initial value of `x`.) The return value has same type and row dimension as xini. Its column dimension is equal to the number of iterations plus 1.

Example
The program below solves the problem ```                 10                -----                          2      minimize  >     [ x  exp(- x  t )  - y  ]   with respect to x                -----    1        2  i      i                i = 1 ```where ```      y  = .5 exp(- t )  and  t  = i / 10       i             i         i ```The solution is `x = {.5, 1.}`. ``` clear # const t = seq(10) / 10. const y = .5 * exp(-t) # function fval(x, fout, dfout) begin      #      fout    =  x(1) * exp(-x(2) * t) - y      if arg(0) < 3 then return      #      d1      =         exp(-x(2) * t)         d2      = -x(1) * exp(-x(2) * t) % t         dfout   = [d1, d2] end xini   = {2., 2.} scale  = {.5, .5} eps    = 1e-4 maxit  = 10 level  = 0 x      = dnlsq(function fval, xini, scale, eps, maxit, level) xout   = x.col(coldim(x)) print  xout ```