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 nlsq Search Tools-> contents reference index search

Nonlinear Least Squares
 Syntax `nlsq(function `f`, `x0`, `scale`, `maxit`, `level`)` See Also nlsqbox , dnlsq

Description
Uses the Gauss-Newton method to solve the problem ```                     2      minimize |f(x)|  with respect to x ```The function call f`(`x`)` returns a column vector with the same type as x0 provided that x has the same type and dimension as x0. The real or double-precision column vector x0 specifies the point at which to start the Gauss-Newton method. The vector scale has the same type and dimension as x0; `scale(i)` is the maximum absolute change in `x(i)` per iteration, and `.0001 scale(i)` is the central difference step size used to approximate the partial derivative of `f` with respect to `x(i)`. The integer scalar maxit specifies the maximum number of iterations to try before giving up on convergence. The integer scalar level specifies the amount of tracing within this function. If `level > 1`, the value of `|f(x)|^2` is printed at each iteration. ``` ```The vector scale also specifies convergence criteria. Convergence is accepted when the absolute change in `x(i)` between iterations is less than `.0001 scale(i)` for all i. ``` ```The return value is a matrix with i-th column equal to the value of x at the beginning of the i-th iteration. The return value has the same type and row dimension as x0. The last column of the return value is the approximate solution.

Example
The following program minimizes the function ```                                 2                       2      f(x) =  [exp(x )  - exp(1)]   + [exp(x )  - exp(2)]                    1                       2 ```with respect to `x`. The resulting value of `x` is `(1, 2)`. ``` clear # function f(x) begin      return {exp(x(1)) - exp(1.), exp(x(2)) - exp(2.)} end level  = 0 maxit  = 20 x0     = {0., 0.} scale  = {1., 1.} result = nlsq(function f, x0, scale, maxit, level) xout   = result.col(coldim(result)) print xout ```