lsqnonlin Solves a non linear data fitting problems. Calling Sequence xopt = lsqnonlin(fun,x0) xopt = lsqnonlin(fun,x0,lb,ub) xopt = lsqnonlin(fun,x0,lb,ub,options) [xopt,resnorm] = lsqnonlin( ... ) [xopt,resnorm,residual] = lsqnonlin( ... ) [xopt,resnorm,residual,exitflag] = lsqnonlin( ... ) [xopt,resnorm,residual,exitflag,output,lambda,gradient] = lsqnonlin( ... ) Input Parameters fun : A function, representing the objective function and gradient (if given) of the problem. x0 : A vector of doubles, containing the starting values of variables of size (1 X n) or (n X 1) where 'n' is the number of variables. lb : A vector of doubles, containing the lower bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of variables. ub : A vector of doubles, containing the upper bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of variables. options : A list, containing the option for user to specify. See below for details. Outputs xopt : A vector of doubles, containing the computed solution of the optimization problem. resnorm : A double, containing the objective value returned as a scalar value i.e. sum(fun(x).^2). residual : A vector of doubles, containing the solution of the objective function, returned as a vector i.e. fun(x). exitflag : An integer, containing the flag which denotes the reason for termination of algorithm. See below for details. output : A structure, containing the information about the optimization. See below for details. lambda : A structure, containing the Lagrange multipliers of the lower bounds, upper bounds and constraints at the optimized point. See below for details. gradient : A vector of doubles, containing the objective's gradient of the solution. Description Search the minimum of a constrained non-linear least square problem specified by : \begin{eqnarray} \hspace{1pt} &\mbox{min}_{x} \hspace{1pt} & (f_{1}(x)^{2} + f_{2}(x)^{2} + f_{n}(x)^{2}) \\ \hspace{1pt} & & lb \leq x \leq ub \\ \end{eqnarray} lsqnonlin calls fmincon, which calls Ipopt, an optimization library written in C++ to solve the non-linear least squares problem. The options should be defined as type "list" and consist of the following fields: options= list("MaxIter", [---], "CpuTime", [---], "GradObj", ---); MaxIter : A Scalar, specifying the maximum number of iterations that the solver should take. CpuTime : A Scalar, specifying the maximum amount of CPU time in seconds that the solver should take. GradObj : A string, representing whetherthe gradient function is on or off. The default values for the various items are given as: Default Values : options = list("MaxIter", [3000], "CpuTime", [600], "GradObj", "off"); The exitflag allows the user to know the status of the optimization which is returned by Ipopt. The values it can take and what they indicate is described below: 0 : Optimal Solution Found 1 : Maximum Number of Iterations Exceeded. Output may not be optimal. 2 : Maximum amount of CPU Time exceeded. Output may not be optimal. 3 : Stop at Tiny Step. 4 : Solved To Acceptable Level. 5 : Converged to a point of local infeasibility. For more details on exitflag, see the Ipopt documentation which can be found on http://www.coin-or.org/Ipopt/documentation/ The output data structure contains detailed information about the optimization process. It is of type "struct" and contains the following fields. output.iterations: The number of iterations performed. output.constrviolation: The max-norm of the constraint violation. The lambda data structure contains the Lagrange multipliers at the end of optimization. In the current version, the values are returned only when the the solution is optimal. It has type "struct" and contains the following fields. lambda.lower: The Lagrange multipliers for the lower bound constraints. lambda.upper: The Lagrange multipliers for the upper bound constraints. A few examples displaying the various functionalities of lsqnonlin have been provided below. You will find a series of problems and the appropriate code snippets to solve them. Example Here we solve a simple non-linear least square example taken from leastsq default present in scilab. Find x in R^2 such that it minimizes: \begin{eqnarray} \mbox{min}_{x}\ f(x) = sum_{i=1,...,n} & wm_{i}^{2} (x_{1} \boldsymbol{\cdot} exp(-x_{2}\boldsymbol{\cdot} tm)) - ym_{i} )^{2}\\ \end{eqnarray}\\ \text{With initial values as: }\\ \begin{array}{cc} [1.5 & 0.8] \end{array} Example Here we build up on the previous example by adding upper and lower bounds to the variables. We add the following bounds to the problem specified above: \begin{eqnarray} -5 &\leq x_{1} &\leq 10\\ -5 &\leq x_{2} &\leq 10\\ \end{eqnarray} Example In this example, we further enhance the functionality of lsqnonlin by setting input options. This provides us with the ability to control the solver parameters such as the maximum number of solver iterations and the max. CPU time allowed for the computation. Authors Harpreet Singh