Solves a non linear data fitting problems.
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( ... )
A function, representing the objective function and gradient (if given) of the problem.
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.
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.
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.
A list, containing the option for user to specify. See below for details.
A vector of doubles, containing the computed solution of the optimization problem.
A double, containing the objective value returned as a scalar value i.e. sum(fun(x).^2).
A vector of doubles, containing the solution of the objective function, returned as a vector i.e. fun(x).
An integer, containing the flag which denotes the reason for termination of algorithm. See below for details.
A structure, containing the information about the optimization. See below for details.
A structure, containing the Lagrange multipliers of the lower bounds, upper bounds and constraints at the optimized point. See below for details.
A vector of doubles, containing the objective's gradient of the solution.
Search the minimum of a constrained non-linear least square problem specified by :
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", ---);
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:
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.
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.
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.
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:
// we have the m measures (ti, yi): m = 10; tm = [0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5]'; ym = [0.79, 0.59, 0.47, 0.36, 0.29, 0.23, 0.17, 0.15, 0.12, 0.08]'; // measure weights (here all equal to 1...) wm = ones(m,1); // and we want to find the parameters x such that the model fits the given // data in the least square sense: // // minimize f(x) = sum_i wm(i)^2 ( x(1)*exp(-x(2)*tm(i) - ym(i) )^2 // initial parameters guess x0 = [1.5 ; 0.8]; // in the first examples, we define the function fun and dfun // in scilab language function y=myfun(x, them, ym, wm) y = wm.*( x(1)*exp(-x(2)*tm) - ym ) endfunction // the simplest call [xopt,resnorm,residual,exitflag,output,lambda,gradient] = lsqnonlin(myfun,x0) // Press ENTER to continue | ![]() | ![]() |
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:
//A simple non-linear least square example taken from leastsq default present in scilab function y=yth(t, x) y = x(1)*exp(-x(2)*t) endfunction // we have the m measures (ti, yi): m = 10; tm = [0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5]'; ym = [0.79, 0.59, 0.47, 0.36, 0.29, 0.23, 0.17, 0.15, 0.12, 0.08]'; // measure weights (here all equal to 1...) wm = ones(m,1); // and we want to find the parameters x such that the model fits the given // data in the least square sense: // // minimize f(x) = sum_i wm(i)^2 ( yth(tm(i),x) - ym(i) )^2 // initial parameters guess x0 = [1.5 ; 0.8]; // in the first examples, we define the function fun and dfun // in scilab language function y=myfun(x, tm, ym, wm) y = wm.*( yth(tm, x) - ym ) endfunction lb = [-5 ,-5]; ub = [10, 10]; // the simplest call [xopt,resnorm,residual,exitflag,output,lambda,gradient] = lsqnonlin(myfun,x0,lb,ub) // Press ENTER to continue | ![]() | ![]() |
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.
//A basic example taken from leastsq default present in scilab with gradient function y=yth(t, x) y = x(1)*exp(-x(2)*t) endfunction // we have the m measures (ti, yi): m = 10; tm = [0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5]'; ym = [0.79, 0.59, 0.47, 0.36, 0.29, 0.23, 0.17, 0.15, 0.12, 0.08]'; // measure weights (here all equal to 1...) wm = ones(m,1); // and we want to find the parameters x such that the model fits the given // data in the least square sense: // // minimize f(x) = sum_i wm(i)^2 ( yth(tm(i),x) - ym(i) )^2 // initial parameters guess x0 = [1.5 ; 0.8]; // in the first examples, we define the function fun and dfun // in scilab language function [y, dy]=myfun(x, tm, ym, wm) y = wm.*( x(1)*exp(-x(2)*tm) - ym ) v = wm.*exp(-x(2)*tm) dy = [v , -x(1)*tm.*v] endfunction lb = [-5,-5]; ub = [10,10]; options = list("GradObj", "on") [xopt,resnorm,residual,exitflag,output,lambda,gradient] = lsqnonlin(myfun,x0,lb,ub,options) | ![]() | ![]() |