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

Nonlinear least Squares With Box Constraints
 Syntax `nlsqbox(function `f`, `xini`, `xlow`, `xup`, `maxit`, `level`)` See Also nlsq , dnlsqb

Description
Uses the Gauss-Newton method to solve the problem ```                       2      minimize   |f(x)|  with respect to x      such that  xlow  < x  < xup                     i    i      i ```The function call f`(`x`)` returns a column vector with the same type as xini provided that x has the same type and dimension as xini. The real or double-precision column vector xini specifies the point at which to start the Gauss-Newton method. The vector xlow specifies the lower limits for the minimization problem and has the same type and dimension as xini. The vector xup specifies the upper limits for the minimization problem and has the same type and dimension as xini. 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 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 xini. The last column of the return value is the approximate solution. ``` ```The central difference step size `.0001[xup(i) - xlow(i)]` is used for approximating the partial derivative of `f` with respect to `x(i)`. Convergence is accepted when the absolute change in `x(i)` between iterations is less than `.0001[xup(i) - xlow(i)]` for all i.

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` and subject to the constraints ```      -1 < x < +1  and  -1 < x < +1            1                 2 ```The optimal value for `x` is `(1, 1)`. ``` clear # function f(x) begin      return {exp(x(1)) - exp(1.), exp(x(2)) - exp(2.)} end level  = 0 maxit  = 20 xini   = {0., 0.} xlow   = {-1., -1.} xup    = {+1., +1.} result = nlsqbox(function f, xini, xlow, xup, maxit, level) xout   = result.col(coldim(result)) print xout ```