Main Content

lsqnonlin

Solve nonlinear least-squares (nonlinear data-fitting) problems

Description

Nonlinear least-squares solver

Solves nonlinear least-squares curve fitting problems of the form

min x f ( x ) 2 2 = min x ( f 1 ( x ) 2 + f 2 ( x ) 2 + ... + f n ( x ) 2 )

subject to the constraints

lb x x ub A x b Aeq x = beq c ( x ) 0 ceq ( x ) = 0.

x,lb, andubcan be vectors or matrices; seeMatrix Arguments.

Do not specify the objective function as the scalar value f ( x ) 2 2 (the sum of squares).lsqnonlinrequires the objective function to be thevector-valued function

f ( x ) = [ f 1 ( x ) f 2 ( x ) f n ( x ) ] .

example

x= lsqnonlin(fun,x0)starts at the pointx0and finds a minimum of the sum of squares of the functions described infun. The functionfunshould return a vector (or array) of values and not the sum of squares of the values. (The algorithm implicitly computes the sum of squares of the components offun(x).)

Note

Passing Extra Parametersexplains how to pass extra parameters to the vector functionfun(x), if necessary.

example

x= lsqnonlin(fun,x0,lb,ub)defines a set of lower and upper bounds on the design variables inx, so that the solution is always in the rangelbxub. You can fix the solution componentx(i)by specifyinglb(i) = ub(i).

Note

If the specified input bounds for a problem are inconsistent, the outputxisx0and the outputsresnormandresidualare[].

Components ofx0that violate the boundslb ≤ x ≤ ubare reset to the interior of the box defined by the bounds. Components that respect the bounds are not changed.

example

x= lsqnonlin(fun,x0,lb,ub,A,b,Aeq,beq)constrains the solution to satisfy the linear constraints

Axb

Aeqx= beq.

example

x= lsqnonlin(fun,x0,lb,ub,A,b,Aeq,beq,nonlcon)constrain the solution to satisfy the nonlinear constraints in thenonlcon(x) function.nonlconreturns two outputs,candceq. The solver attempts to satisfy the constraints

c≤ 0

ceq= 0.

example

x= lsqnonlin(fun,x0,lb,ub,options)andx= lsqnonlin(fun,x0,lb,ub,A,b,Aeq,beq,nonlcon,options)minimizes with the optimization options specified inoptions. Useoptimoptionsto set these options. Pass empty matrices forlbanduband for other input arguments if the arguments do not exist.

x= lsqnonlin(problem)finds the minimum forproblem, a structure described inproblem.

example

[x,resnorm] = lsqnonlin(___), for any input arguments, returns the value of the squared 2-norm of the residual atx:sum(fun(x).^2).

example

[x,resnorm,residual,exitflag,output] = lsqnonlin(___)additionally returns the value of the residualfun(x)at the solutionx, a valueexitflagthat describes the exit condition, and a structureoutputthat contains information about the optimization process.

[x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqnonlin(___)additionally returns a structurelambdawhose fields contain the Lagrange multipliers at the solutionx, and the Jacobian offunat the solutionx.

Examples

collapse all

Fit a simple exponential decay curve to data.

Generate data from an exponential decay model plus noise. The model is

y = exp ( - 1 . 3 t ) + ε ,

with t ranging from 0 through 3, and ε normally distributed noise with mean 0 and standard deviation 0.05.

rngdefault% for reproducibilityd = linspace(0,3); y = exp(-1.3*d) + 0.05*randn(size(d));

The problem is: given the data (d,y), find the exponential decay rate that best fits the data.

Create an anonymous function that takes a value of the exponential decay rate r and returns a vector of differences from the model with that decay rate and the data.

fun = @(r)exp(-d*r)-y;

Find the value of the optimal decay rate. Arbitrarily choose an initial guessx0= 4.

x0 = 4; x = lsqnonlin(fun,x0)
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
x = 1.2645

Plot the data and the best-fitting exponential curve.

plot(d,y,'ko',d,exp(-x*d),'b-') legend('Data','Best fit') xlabel('t') ylabel('exp(-tx)')

Figure contains an axes object. The axes object with xlabel t, ylabel exp(-tx) contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Best fit.

Find the best-fitting model when some of the fitting parameters have bounds.

Find a centering b and scaling a that best fit the function

a exp ( - t ) exp ( - exp ( - ( t - b ) ) )

to the standard normal density,

1 2 π exp ( - t 2 / 2 ) .

Create a vectortof data points, and the corresponding normal density at those points.

t = linspace(-4,4); y = 1/sqrt(2*pi)*exp(-t.^2/2);

Create a function that evaluates the difference between the centered and scaled function from the normaly, withx(1)as the scaling a andx(2)as the centering b .

fun = @(x)x(1)*exp(-t).*exp(-exp(-(t-x(2)))) - y;

Find the optimal fit starting fromx0=[1/2,0], with the scaling a between 1/2 and 3/2, and the centering b between -1 and 3.

磅= [1/2,1];乌兰巴托= (3/2,3);x0 = [1 /2,0]; x = lsqnonlin(fun,x0,lb,ub)
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
x =1×20.8231 -0.2444

Plot the two functions to see the quality of the fit.

plot(t,y,'r-',t,fun(x)+y,'b-') xlabel('t') legend('Normal density','Fitted function')

Figure contains an axes object. The axes object with xlabel t contains 2 objects of type line. These objects represent Normal density, Fitted function.

Consider the following objective function, a sum of squares:

k = 1 1 0 ( 2 + 2 k + exp ( k x 1 ) + 2 exp ( 2 k x 2 2 ) ) 2 .

The code for this objective function appears as themyfunfunction at theend of this example.

Minimize this function subject to the linear constraint x 1 x 2 2 . Write this constraint as x 1 - x 2 2 0 .

A = [1 -1/2]; b = 0;

Impose the bounds x 1 0 , x 2 0 , x 1 2 , and x 2 4 .

lb = [0 0]; ub = [2 4];

Start the optimization process from the pointx0 = [0.3 0.4].

x0 = [0.3 0.4];

The problem has no linear equality constraints.

Aeq = []; beq = [];

Run the optimization.

x = lsqnonlin(@myfun,x0,lb,ub,A,b,Aeq,beq)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x =1×20.1695 0.3389
functionF = myfun(x) k = 1:10; F = 2 + 2*k - exp(k*x(1)) - 2*exp(2*k*(x(2)^2));end

Consider the following objective function, a sum of squares:

k = 1 1 0 ( 2 + 2 k + exp ( k x 1 ) + 2 exp ( 2 k x 2 2 ) ) 2 .

The code for this objective function appears as themyfunfunction at theend of this example.

Minimize this function subject to the nonlinear constraint ( x 1 ) cos ( x 2 ) . The code for this nonlinear constraint function appears as thenlconfunction at theend of this example.

Impose the bounds x 1 0 , x 2 0 , x 1 2 , and x 2 4 .

lb = [0 0]; ub = [2 4];

Start the optimization process from the pointx0 = [0.3 0.4].

x0 = [0.3 0.4];

The problem has no linear constraints.

A = []; b = []; Aeq = []; beq = [];

Run the optimization.

x = lsqnonlin(@myfun,x0,lb,ub,A,b,Aeq,beq,@nlcon)
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
x =1×20.2133 0.3266
functionF = myfun(x) k = 1:10; F = 2 + 2*k - exp(k*x(1)) - 2*exp(2*k*(x(2)^2));endfunction[c,ceq] = nlcon(x) ceq = []; c = sin(x(1)) - cos(x(2));end

Compare the results of a data-fitting problem when using differentlsqnonlinalgorithms.

Suppose that you have observation time dataxdataand observed response dataydata, and you want to find parameters x ( 1 ) and x ( 2 ) to fit a model of the form

ydata = x ( 1 ) exp ( x ( 2 ) xdata ) .

Input the observation times and responses.

xdata =...[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3]; ydata =...[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];

Create a simple exponential decay model. The model computes a vector of differences between predicted values and observed values.

fun = @(x)x(1)*exp(x(2)*xdata)-ydata;

Fit the model using the starting pointx0 = [100,-1]. First, use the default'trust-region-reflective'algorithm.

x0 = [100,-1]; options = optimoptions(@lsqnonlin,'Algorithm','trust-region-reflective'); x = lsqnonlin(fun,x0,[],[],options)
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
x =1×2498.8309 -0.1013

See if there is any difference using the'levenberg-marquardt'algorithm.

options.Algorithm ='levenberg-marquardt';x = lsqnonlin(有趣,x0,[]、[]选项)
Local minimum possible. lsqnonlin stopped because the relative size of the current step is less than the value of the step size tolerance.
x =1×2498.8309 -0.1013

The two algorithms found the same solution. Plot the solution and the data.

plot(xdata,ydata,'ko') holdontlist = linspace(xdata(1),xdata(end)); plot(tlist,x(1)*exp(x(2)*tlist),'b-') xlabelxdataylabelydatatitle('Exponential Fit to Data') legend('Data','Exponential Fit') holdoff

Figure contains an axes object. The axes object with title Exponential Fit to Data, xlabel xdata, ylabel ydata contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Exponential Fit.

Find the x that minimizes

k = 1 1 0 ( 2 + 2 k - e k x 1 - e k x 2 ) 2 ,

and find the value of the minimal sum of squares.

Becauselsqnonlinassumes that the sum of squares is not explicitly formed in the user-defined function, the function passed tolsqnonlinshould instead compute the vector-valued function

F k ( x ) = 2 + 2 k - e k x 1 - e k x 2 ,

for k = 1 to 1 0 (that is, F should have 1 0 components).

Themyfunfunction, which computes the 10-component vector F, appears at theend of this example.

Find the minimizing point and the minimum value, starting at the pointx0 = [0.3,0.4].

x0 = [0.3,0.4]; [x,resnorm] = lsqnonlin(@myfun,x0)
Local minimum possible. lsqnonlin stopped because the size of the current step is less than the value of the step size tolerance.
x =1×20.2578 0.2578
resnorm = 124.3622

Theresnormoutput is the squared residual norm, or the sum of squares of the function values.

The following function computes the vector-valued objective function.

functionF = myfun(x) k = 1:10; F = 2 + 2*k-exp(k*x(1))-exp(k*x(2));end

Examine the solution process both as it occurs (by setting theDisplayoption to'iter') and afterward (by examining theoutputstructure).

Suppose that you have observation time dataxdataand observed response dataydata, and you want to find parameters x ( 1 ) and x ( 2 ) to fit a model of the form

ydata = x ( 1 ) exp ( x ( 2 ) xdata ) .

Input the observation times and responses.

xdata =...[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3]; ydata =...[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];

Create a simple exponential decay model. The model computes a vector of differences between predicted values and observed values.

fun = @(x)x(1)*exp(x(2)*xdata)-ydata;

Fit the model using the starting pointx0 = [100,-1]. Examine the solution process by setting theDisplayoption to'iter'. Obtain anoutputstructure to obtain more information about the solution process.

x0 = [100,-1]; options = optimoptions('lsqnonlin','Display','iter'); [x,resnorm,residual,exitflag,output] = lsqnonlin(fun,x0,[],[],options);
Norm of First-order Iteration Func-count Resnorm step optimality 0 3 359677 2.88e+04 Objective function returned Inf; trying a new point... 1 6 359677 11.6976 2.88e+04 2 9 321395 0.5 4.97e+04 3 12 321395 1 4.97e+04 4 15 292253 0.25 7.06e+04 5 18 292253 0.5 7.06e+04 6 21 270350 0.125 1.15e+05 7 24 270350 0.25 1.15e+05 8 27 252777 0.0625 1.63e+05 9 30 252777 0.125 1.63e+05 10 33 243877 0.03125 7.48e+04 11 36 243660 0.0625 8.7e+04 12 39 243276 0.0625 2e+04 13 42 243174 0.0625 1.14e+04 14 45 242999 0.125 5.1e+03 15 48 242661 0.25 2.04e+03 16 51 241987 0.5 1.91e+03 17 54 240643 1 1.04e+03 18 57 237971 2 3.36e+03 19 60 232686 4 6.04e+03 20 63 222354 8 1.2e+04 21 66 202592 16 2.25e+04 22 69 166443 32 4.05e+04 23 72 106320 64 6.68e+04 24 75 28704.7 128 8.31e+04 25 78 89.7947 140.674 2.22e+04 26 81 9.57381 2.02599 684 27 84 9.50489 0.0619927 2.27 28 87 9.50489 0.000462261 0.0114 Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.

Examine the output structure to obtain more information about the solution process.

output
output =struct with fields:firstorderopt: 0.0114 iterations: 28 funcCount: 87 cgiterations: 0 algorithm: 'trust-region-reflective' stepsize: 4.6226e-04 message: 'Local minimum possible....' bestfeasible: [] constrviolation: []

For comparison, set theAlgorithmoption to'levenberg-marquardt'.

options.Algorithm ='levenberg-marquardt';[x,resnorm,residual,exitflag,output] = lsqnonlin(fun,x0,[],[],options);
First-order Norm of Iteration Func-count Resnorm optimality Lambda step 0 3 359677 2.88e+04 0.01 Objective function returned Inf; trying a new point... 1 13 340761 3.91e+04 100000 0.280777 2 16 304661 5.97e+04 10000 0.373146 3 21 297292 6.55e+04 1e+06 0.0589933 4 24 288240 7.57e+04 100000 0.0645444 5 28 275407 1.01e+05 1e+06 0.0741266 6 31 249954 1.62e+05 100000 0.094571 7 36 245896 1.35e+05 1e+07 0.0133606 8 39 243846 7.26e+04 1e+06 0.0094431 9 42 243568 5.66e+04 100000 0.0082162 10 45 243424 1.61e+04 10000 0.00777935 11 48 243322 8.8e+03 1000 0.0673933 12 51 242408 5.1e+03 100 0.675209 13 54 233628 1.05e+04 10 6.59804 14 57 169089 8.51e+04 1 54.6992 15 60 30814.7 1.54e+05 0.1 196.939 16 63 147.496 8e+03 0.01 129.795 17 66 9.51503 117 0.001 9.96069 18 69 9.50489 0.0714 0.0001 0.080486 19 72 9.50489 5.23e-05 1e-05 5.07043e-05 Local minimum possible. lsqnonlin stopped because the relative size of the current step is less than the value of the step size tolerance.

The'levenberg-marquardt'converged with fewer iterations, but almost as many function evaluations:

output
output =struct with fields:iterations: 19 funcCount: 72 stepsize: 5.0704e-05 cgiterations: [] firstorderopt: 5.2319e-05 algorithm: 'levenberg-marquardt' message: 'Local minimum possible....' bestfeasible: [] constrviolation: []

Input Arguments

collapse all

Function whose sum of squares is minimized, specified as a function handle or the name of a function. For the'interior-point'algorithm,fun必须是一个function handle.funis a function that accepts an arrayxand returns an arrayF, the objective function evaluated atx.

Note

The sum of squares should not be formed explicitly. Instead, your function should return a vector of function values. SeeExamples.

The functionfuncan be specified as a function handle to a file:

x = lsqnonlin(@myfun,x0)

wheremyfunis a MATLAB®function such as

functionF = myfun(x) F =...% Compute function values at x

funcan also be a function handle for an anonymous function.

x = lsqnonlin(@(x)sin(x.*x),x0);

lsqnonlinpassesxto your objective function in the shape of thex0论点. For example, ifx0is a 5-by-3 array, thenlsqnonlinpassesxtofunas a 5-by-3 array.

If the Jacobian can also be computedandthe'SpecifyObjectiveGradient'option istrue, set by

options = optimoptions('lsqnonlin','SpecifyObjectiveGradient',true)

then the functionfunmust return a second output argument with the Jacobian valueJ(a matrix) atx. By checking the value ofnargout, the function can avoid computingJwhenfunis called with only one output argument (in the case where the optimization algorithm only needs the value ofFbut notJ).

function[F,J] = myfun(x) F =...% Objective function values at xifnargout > 1% Two output argumentsJ =...% Jacobian of the function evaluated at xend

Iffunreturns an array ofmcomponents andxhasnelements, wherenis the number of elements ofx0, the JacobianJis anm-by-nmatrix whereJ(i,j)is the partial derivative ofF(i)with respect tox(j). (The JacobianJis the transpose of the gradient ofF.)

Example:@(x)cos(x).*exp(-x)

Data Types:char|function_handle|string

Initial point, specified as a real vector or real array. Solvers use the number of elements inx0and the size ofx0to determine the number and size of variables thatfunaccepts.

Example:x0 = [1,2,3,4]

Data Types:double

Lower bounds, specified as a real vector or real array. If the number of elements inx0is equal to the number of elements inlb, thenlbspecifies that

x(i) >= lb(i)for alli.

Ifnumel(lb) < numel(x0), thenlbspecifies that

x(i) >= lb(i)for1 <= i <= numel(lb).

Iflbhas fewer elements thanx0, solvers issue a warning.

Example:To specify that all x components are positive, uselb = zeros(size(x0)).

Data Types:double

Upper bounds, specified as a real vector or real array. If the number of elements inx0is equal to the number of elements inub, thenubspecifies that

x(i) <= ub(i)for alli.

Ifnumel(ub) < numel(x0), thenubspecifies that

x(i) <= ub(i)for1 <= i <= numel(ub).

Ifubhas fewer elements thanx0, solvers issue a warning.

Example:To specify that all x components are less than 1, useub = ones(size(x0)).

Data Types:double

Linear inequality constraints, specified as a real matrix.Ais anM-by-Nmatrix, whereMis the number of inequalities, andNis the number of variables (number of elements inx0). For large problems, passAas a sparse matrix.

Aencodes theMlinear inequalities

A*x <= b,

wherexis the column vector ofNvariablesx(:), andbis a column vector withMelements.

For example, consider these inequalities:

x1+ 2x2≤ 10
3x1+ 4x2≤ 20
5x1+ 6x2≤ 30,

Specify the inequalities by entering the following constraints.

A = [1,2;3,4;5,6]; b = [10;20;30];

Example:To specify that the x components sum to 1 or less, useA = ones(1,N)andb = 1.

Data Types:double

Linear inequality constraints, specified as a real vector.bis anM-element vector related to theAmatrix. If you passbas a row vector, solvers internally convertbto the column vectorb(:). For large problems, passbas a sparse vector.

bencodes theMlinear inequalities

A*x <= b,

wherexis the column vector ofNvariablesx(:), andAis a matrix of sizeM-by-N.

For example, consider these inequalities:

x1+ 2x2≤ 10
3x1+ 4x2≤ 20
5x1+ 6x2≤ 30.

Specify the inequalities by entering the following constraints.

A = [1,2;3,4;5,6]; b = [10;20;30];

Example:To specify that the x components sum to 1 or less, useA = ones(1,N)andb = 1.

Data Types:double

线性等式约束,指定为一个真正的matrix.Aeqis anMe-by-Nmatrix, whereMeis the number of equalities, andNis the number of variables (number of elements inx0). For large problems, passAeqas a sparse matrix.

Aeqencodes theMelinear equalities

Aeq*x = beq,

wherexis the column vector ofNvariablesx(:), andbeqis a column vector withMeelements.

For example, consider these inequalities:

x1+ 2x2+ 3x3= 10
2x1+ 4x2+x3= 20,

Specify the inequalities by entering the following constraints.

Aeq = [1,2,3;2,4,1]; beq = [10;20];

Example:To specify that the x components sum to 1, useAeq = ones(1,N)andbeq = 1.

Data Types:double

Linear equality constraints, specified as a real vector.beqis anMe-element vector related to theAeqmatrix. If you passbeqas a row vector, solvers internally convertbeqto the column vectorbeq(:). For large problems, passbeqas a sparse vector.

beqencodes theMelinear equalities

Aeq*x = beq,

wherexis the column vector ofNvariablesx(:), andAeqis a matrix of sizeMe-by-N.

For example, consider these equalities:

x1+ 2x2+ 3x3= 10
2x1+ 4x2+x3= 20.

Specify the equalities by entering the following constraints.

Aeq = [1,2,3;2,4,1]; beq = [10;20];

Example:To specify that the x components sum to 1, useAeq = ones(1,N)andbeq = 1.

Data Types:double

Nonlinear constraints, specified as a function handle.nonlconis a function that accepts a vector or arrayxand returns two arrays,c(x)andceq(x).

  • c(x)is the array of nonlinear inequality constraints atx.lsqnonlinattempts to satisfy

    c(x) <= 0for all entries ofc. (1)
  • ceq(x)is the array of nonlinear equality constraints atx.lsqnonlinattempts to satisfy

    ceq(x) = 0for all entries ofceq. (2)

For example,

x = lsqnonlin(@myfun,x0,lb,ub,A,b,Aeq,beq,@mycon,options)

wheremyconis a MATLAB function such as

function[c,ceq] = mycon(x) c =...% Compute nonlinear inequalities at x.ceq =...% Compute nonlinear equalities at x.
If the Jacobians (derivatives) of the constraints can also be computedandtheSpecifyConstraintGradientoption istrue, as set by
options = optimoptions('','SpecifyConstraintGradient',true)
thennonlconmust also return, in the third and fourth output arguments,GC, the Jacobian ofc(x), andGCeq, the Jacobian ofceq(x). The JacobianG(x) of a vector functionF(x) is

G i , j ( x ) = F i ( x ) x j .

GCandGCeqcan be sparse or dense. IfGCorGCeqis large, with relatively few nonzero entries, save running time and memory in the'interior-point'algorithm by representing them as sparse matrices. For more information, seeNonlinear Constraints.

Data Types:function_handle

Optimization options, specified as the output ofoptimoptionsor a structure asoptimsetreturns.

Some options apply to all algorithms, and others are relevant for particular algorithms. SeeOptimization Options Referencefor detailed information.

Some options are absent from theoptimoptionsdisplay. These options appear in italics in the following table. For details, seeView Optimization Options.

All Algorithms

Algorithm

Choose between'trust-region-reflective'(default),'levenberg-marquardt', and'interior-point'.

TheAlgorithmoption specifies a preference for which algorithm to use. It is only a preference, because certain conditions must be met to use each algorithm. For the trust-region-reflective algorithm, the number of elements ofFreturned byfunmust be at least as many as the length ofx.

The'interior-point'algorithm is the only algorithm that can solve problems with linear or nonlinear constraints. If you include these constraints in your problem and do not specify an algorithm, the solver automatically switches to the'interior-point'algorithm. The'interior-point'algorithm calls a modified version of thefmincon'interior-point'algorithm.

For more information on choosing the algorithm, seeChoosing the Algorithm.

CheckGradients

Compare user-supplied derivatives (gradients of objective or constraints) to finite-differencing derivatives. Choices arefalse(default) ortrue.

Foroptimset, the name isDerivativeCheckand the values are'on'or'off'. SeeCurrent and Legacy Option Names.

Diagnostics

Display diagnostic information about the function to be minimized or solved. Choices are'off'(default) or'on'.

DiffMaxChange

Maximum change in variables for finite-difference gradients (a positive scalar). The default isInf.

DiffMinChange

Minimum change in variables for finite-difference gradients (a positive scalar). The default is0.

Display

Level of display (seeIterative Display):

  • 'off'or'none'displays no output.

  • 'iter'displays output at each iteration, and gives the default exit message.

  • 'iter-detailed'displays output at each iteration, and gives the technical exit message.

  • 'final'(default) displays just the final output, and gives the default exit message.

  • 'final-detailed'displays just the final output, and gives the technical exit message.

FiniteDifferenceStepSize

Scalar or vector step size factor for finite differences. When you setFiniteDifferenceStepSizeto a vectorv, the forward finite differencesdeltaare

delta = v.*sign′(x).*max(abs(x),TypicalX);

wheresign′(x) = sign(x)exceptsign′(0) = 1. Central finite differences are

delta = v.*max(abs(x),TypicalX);

ScalarFiniteDifferenceStepSizeexpands to a vector. The default issqrt(eps)for forward finite differences, andeps^(1/3)for central finite differences.

Foroptimset, the name isFinDiffRelStep. SeeCurrent and Legacy Option Names.

FiniteDifferenceType

Finite differences, used to estimate gradients, are either'forward'(default), or'central'(centered).'central'takes twice as many function evaluations, but should be more accurate.

The algorithm is careful to obey bounds when estimating both types of finite differences. So, for example, it could take a backward, rather than a forward, difference to avoid evaluating at a point outside bounds.

Foroptimset, the name isFinDiffType. SeeCurrent and Legacy Option Names.

FunctionTolerance

Termination tolerance on the function value, a positive scalar. The default is1e-6. SeeTolerances and Stopping Criteria.

Foroptimset, the name isTolFun. SeeCurrent and Legacy Option Names.

FunValCheck

Check whether function values are valid.'on'displays an error when the function returns a value that iscomplex,Inf, orNaN. The default'off'displays no error.

MaxFunctionEvaluations

Maximum number of function evaluations allowed, a positive integer. The default is100*numberOfVariablesfor the'trust-region-reflective'algorithm,200*numberOfVariablesfor the'levenberg-marquardt'algorithm, and3000for the'interior-point'algorithm. SeeTolerances and Stopping CriteriaandIterations and Function Counts.

Foroptimset, the name isMaxFunEvals. SeeCurrent and Legacy Option Names.

MaxIterations

Maximum number of iterations allowed, a positive integer. The default is400for the'trust-region-reflective'and'levenberg-marquardt'algorithms, and1000for the'interior-point'algorithm. SeeTolerances and Stopping CriteriaandIterations and Function Counts.

Foroptimset, the name isMaxIter. SeeCurrent and Legacy Option Names.

OptimalityTolerance

Termination tolerance on the first-order optimality (a positive scalar). The default is1e-6. SeeFirst-Order Optimality Measure.

Internally, the'levenberg-marquardt'algorithm uses an optimality tolerance (stopping criterion) of1e-4timesFunctionToleranceand does not useOptimalityTolerance.

Foroptimset, the name isTolFun. SeeCurrent and Legacy Option Names.

OutputFcn

Specify one or more user-defined functions that an optimization function calls at each iteration. Pass a function handle or a cell array of function handles. The default is none ([]). SeeOutput Function and Plot Function Syntax.

PlotFcn

情节各种措施的进展而寒冷ithm executes; select from predefined plots or write your own. Pass a name, a function handle, or a cell array of names or function handles. For custom plot functions, pass function handles. The default is none ([]):

  • 'optimplotx'plots the current point.

  • 'optimplotfunccount'plots the function count.

  • 'optimplotfval'plots the function value.

  • 'optimplotresnorm'plots the norm of the residuals.

  • 'optimplotstepsize'plots the step size.

  • 'optimplotfirstorderopt'plots the first-order optimality measure.

Custom plot functions use the same syntax as output functions. SeeOutput Functions for Optimization ToolboxandOutput Function and Plot Function Syntax.

Foroptimset, the name isPlotFcns. SeeCurrent and Legacy Option Names.

SpecifyObjectiveGradient

Iffalse(default), the solver approximates the Jacobian using finite differences. Iftrue, the solver uses a user-defined Jacobian (defined infun), or Jacobian information (when usingJacobMult), for the objective function.

Foroptimset, the name isJacobian, and the values are'on'or'off'. SeeCurrent and Legacy Option Names.

StepTolerance

Termination tolerance onx, a positive scalar. The default is1e-6for the'trust-region-reflective'and'levenberg-marquardt'algorithms, and1e-10for the'interior-point'algorithm. SeeTolerances and Stopping Criteria.

Foroptimset, the name isTolX. SeeCurrent and Legacy Option Names.

TypicalX

Typicalxvalues. The number of elements inTypicalXis equal to the number of elements inx0, the starting point. The default value isones(numberofvariables,1). The solver usesTypicalXfor scaling finite differences for gradient estimation.

UseParallel

Whentrue, the solver estimates gradients in parallel. Disable by setting to the default,false. SeeParallel Computing.

Trust-Region-Reflective Algorithm
JacobianMultiplyFcn

Jacobian multiply function, specified as a function handle. For large-scale structured problems, this function computes the Jacobian matrix productJ*Y,J'*Y, orJ'*(J*Y)without actually formingJ. The function is of the form

W = jmfun(Jinfo,Y,flag)

whereJinfocontains the matrix used to computeJ*Y(orJ'*Y, orJ'*(J*Y)). The first argumentJinfomust be the same as the second argument returned by the objective functionfun, for example, by

[F,Jinfo] = fun(x)

Yis a matrix that has the same number of rows as there are dimensions in the problem.flagdetermines which product to compute:

  • Ifflag == 0thenW = J'*(J*Y).

  • Ifflag > 0thenW = J*Y.

  • Ifflag < 0thenW = J'*Y.

In each case,Jis not formed explicitly. The solver usesJinfoto compute the preconditioner. SeePassing Extra Parametersfor information on how to supply values for any additional parametersjmfunneeds.

Note

'SpecifyObjectiveGradient'must be set totruefor the solver to passJinfofromfuntojmfun.

SeeMinimization with Dense Structured Hessian, Linear EqualitiesandJacobian Multiply Function with Linear Least Squares类似的examples.

Foroptimset, the name isJacobMult. SeeCurrent and Legacy Option Names.

JacobPattern

Sparsity pattern of the Jacobian for finite differencing. SetJacobPattern(i,j) = 1whenfun(i)depends onx(j). Otherwise, setJacobPattern(i,j) = 0. In other words,JacobPattern(i,j) = 1when you can have ∂fun(i)/∂x(j)≠ 0.

UseJacobPatternwhen it is inconvenient to compute the Jacobian matrixJinfun, though you can determine (say, by inspection) whenfun(i)depends onx(j). The solver can approximateJvia sparse finite differences when you giveJacobPattern.

If the structure is unknown, do not setJacobPattern. The default behavior is as ifJacobPatternis a dense matrix of ones. Then the solver computes a full finite-difference approximation in each iteration. This can be expensive for large problems, so it is usually better to determine the sparsity structure.

MaxPCGIter

Maximum number of PCG (preconditioned conjugate gradient) iterations, a positive scalar. The default ismax(1,numberOfVariables/2). For more information, seeLarge Scale Nonlinear Least Squares.

PrecondBandWidth

Upper bandwidth of preconditioner for PCG, a nonnegative integer. The defaultPrecondBandWidthisInf, which means a direct factorization (Cholesky) is used rather than the conjugate gradients (CG). The direct factorization is computationally more expensive than CG, but produces a better quality step towards the solution. SetPrecondBandWidthto0for diagonal preconditioning (upper bandwidth of 0). For some problems, an intermediate bandwidth reduces the number of PCG iterations.

SubproblemAlgorithm

Determines how the iteration step is calculated. The default,'factorization', takes a slower but more accurate step than'cg'. SeeTrust-Region-Reflective Least Squares.

TolPCG

Termination tolerance on the PCG iteration, a positive scalar. The default is0.1.

Levenberg-Marquardt Algorithm
InitDamping

Initial value of the Levenberg-Marquardt parameter, a positive scalar. Default is1e-2. For details, seeLevenberg-Marquardt Method.

ScaleProblem

'jacobian'有时可以改善收敛性很差吗scaled problem; the default is'none'.

Interior-Point Algorithm
BarrierParamUpdate

Specifies howfminconupdates the barrier parameter (seefmincon Interior Point Algorithm). The options are:

  • 'monotone'(default)

  • 'predictor-corrector'

This option can affect the speed and convergence of the solver, but the effect is not easy to predict.

ConstraintTolerance

Tolerance on the constraint violation, a positive scalar. The default is1e-6. SeeTolerances and Stopping Criteria.

Foroptimset, the name isTolCon. SeeCurrent and Legacy Option Names.

InitBarrierParam

Initial barrier value, a positive scalar. Sometimes it might help to try a value above the default0.1, especially if the objective or constraint functions are large.

SpecifyConstraintGradient

Gradient for nonlinear constraint functions defined by the user. When set to the default,false,lsqnonlinestimates gradients of the nonlinear constraints by finite differences. When set totrue,lsqnonlinexpects the constraint function to have four outputs, as described innonlcon.

Foroptimset, the name isGradConstrand the values are'on'or'off'. SeeCurrent and Legacy Option Names.

SubproblemAlgorithm

Determines how the iteration step is calculated. The default,'factorization', is usually faster than'cg'(conjugate gradient), though'cg'might be faster for large problems with dense Hessians. Seefmincon Interior Point Algorithm.

Foroptimset, the values are'cg'and'ldl-factorization'. SeeCurrent and Legacy Option Names.

Example:options = optimoptions('lsqnonlin','FiniteDifferenceType','central')

Problem structure, specified as a structure with the following fields:

Field Name Entry

objective

Objective function

x0

Initial point forx

Aineq

Matrix for linear inequality constraints

bineq

Vector for linear inequality constraints

Aeq

矩阵线性等式约束

beq

Vector for linear equality constraints
lb Vector of lower bounds
ub Vector of upper bounds

nonlcon

Nonlinear constraint function

solver

'lsqnonlin'

options

Options created withoptimoptions

You must supply at least theobjective,x0,solver, andoptionsfields in theproblemstructure.

Data Types:struct

Output Arguments

collapse all

Solution, returned as a real vector or real array. The size ofxis the same as the size ofx0. Typically,xis a local solution to the problem whenexitflagis positive. For information on the quality of the solution, seeWhen the Solver Succeeds.

Squared norm of the residual, returned as a nonnegative real.resnormis the squared 2-norm of the residual atx:sum(fun(x).^2).

Value of objective function at solution, returned as an array. In general,residual = fun(x).

Reason the solver stopped, returned as an integer.

1

Function converged to a solutionx.

2

Change inxis less than the specified tolerance, or Jacobian atxis undefined.

3

Change in the residual is less than the specified tolerance.

4

Relative magnitude of search direction is smaller than the step tolerance.

0

Number of iterations exceedsoptions.MaxIterationsor number of function evaluations exceededoptions.MaxFunctionEvaluations.

-1

A plot function or output function stopped the solver.

-2

No feasible point found. The boundslbandubare inconsistent, or the solver stopped at an infeasible point.

Information about the optimization process, returned as a structure with fields:

firstorderopt

Measure of first-order optimality

iterations

Number of iterations taken

funcCount

The number of function evaluations

cgiterations

Total number of PCG iterations ('trust-region-reflective'and'interior-point'algorithms)

stepsize

Final displacement inx

constrviolation

Maximum of constraint functions ('interior-point'algorithm)

bestfeasible

Best (lowest objective function) feasible point encountered ('interior-point'algorithm). A structure with these fields:

  • x

  • fval

  • firstorderopt

  • constrviolation

If no feasible point is found, thebestfeasiblefield is empty. For this purpose, a point is feasible when the maximum of the constraint functions does not exceedoptions.ConstraintTolerance.

Thebestfeasiblepoint can differ from the returned solution pointxfor a variety of reasons. For an example, seeObtain Best Feasible Point.

algorithm

Optimization algorithm used

message

Exit message

Lagrange multipliers at the solution, returned as a structure with fields:

lower

Lower bounds corresponding tolb

upper

Upper bounds corresponding toub

ineqlin

Linear inequalities corresponding toAandb

eqlin

Linear equalities corresponding toAeqandbeq

ineqnonlin

Nonlinear inequalities corresponding to thecinnonlcon

eqnonlin

Nonlinear equalities corresponding to theceqinnonlcon

Jacobian at the solution, returned as a real matrix.jacobian(i,j)is the partial derivative offun(i)with respect tox(j)at the solutionx.

For problems with active constraints at the solution,jacobianis not useful for estimating confidence intervals.

Limitations

  • The trust-region-reflective algorithm does not solve underdetermined systems; it requires that the number of equations, i.e., the row dimension ofF, be at least as great as the number of variables. In the underdetermined case,lsqnonlinuses the Levenberg-Marquardt algorithm.

  • lsqnonlincan solve complex-valued problems directly. Note that constraints do not make sense for complex values, because complex numbers are not well-ordered; asking whether one complex value is greater or less than another complex value is nonsensical. For a complex problem with bound constraints, split the variables into real and imaginary parts. Do not use the'interior-point'algorithm with complex data. SeeFit a Model to Complex-Valued Data.

  • The preconditioner computation used in the preconditioned conjugate gradient part of the trust-region-reflective method formsJTJ(whereJis the Jacobian matrix) before computing the preconditioner. Therefore, a row ofJwith many nonzeros, which results in a nearly dense productJTJ, can lead to a costly solution process for large problems.

  • If components ofxhave no upper (or lower) bounds,lsqnonlinprefers that the corresponding components ofub(orlb) be set toinf(or-inffor lower bounds) as opposed to an arbitrary but very large positive (or negative for lower bounds) number.

You can use the trust-region reflective algorithm inlsqnonlin,lsqcurvefit, andfsolvewith small- to medium-scale problems without computing the Jacobian infunor providing the Jacobian sparsity pattern. (This also applies to usingfminconorfminuncwithout computing the Hessian or supplying the Hessian sparsity pattern.) How small is small- to medium-scale? No absolute answer is available, as it depends on the amount of virtual memory in your computer system configuration.

Suppose your problem hasmequations andnunknowns. If the commandJ = sparse(ones(m,n))causes anOut of memoryerror on your machine, then this is certainly too large a problem. If it does not result in an error, the problem might still be too large. You can find out only by running it and seeing if MATLAB runs within the amount of virtual memory available on your system.

Algorithms

The Levenberg-Marquardt and trust-region-reflective methods are based on the nonlinear least-squares algorithms also used infsolve.

  • The default trust-region-reflective algorithm is a subspace trust-region method and is based on the interior-reflective Newton method described in[1]and[2]. Each iteration involves the approximate solution of a large linear system using the method of preconditioned conjugate gradients (PCG). SeeTrust-Region-Reflective Least Squares.

  • Levenberg-Marquardt方法中描述的裁判erences[4],[5], and[6]. SeeLevenberg-Marquardt Method.

The'interior-point'algorithm uses thefmincon'interior-point'algorithm with some modifications. For details, seeModified fmincon Algorithm for Constrained Least Squares.

Alternative Functionality

App

TheOptimizeLive Editor task provides a visual interface forlsqnonlin.

References

[1] Coleman, T.F. and Y. Li. “An Interior, Trust Region Approach for Nonlinear Minimization Subject to Bounds.”SIAM Journal on Optimization, Vol. 6, 1996, pp. 418–445.

[2] Coleman, T.F. and Y. Li. “On the Convergence of Reflective Newton Methods for Large-Scale Nonlinear Minimization Subject to Bounds.”Mathematical Programming, Vol. 67, Number 2, 1994, pp. 189–224.

[3] Dennis, J. E. Jr. “Nonlinear Least-Squares.”State of the Art in Numerical Analysis, ed. D. Jacobs, Academic Press, pp. 269–312.

[4] Levenberg, K. “A Method for the Solution of Certain Problems in Least-Squares.”Quarterly Applied Mathematics 2, 1944, pp. 164–168.

[5] Marquardt, D. “An Algorithm for Least-squares Estimation of Nonlinear Parameters.”SIAM Journal Applied Mathematics, Vol. 11, 1963, pp. 431–441.

[6] Moré, J. J. “The Levenberg-Marquardt Algorithm: Implementation and Theory.”Numerical Analysis, ed. G. A. Watson, Lecture Notes in Mathematics 630, Springer Verlag, 1977, pp. 105–116.

[7] Moré, J. J., B. S. Garbow, and K. E. Hillstrom.User Guide for MINPACK 1. Argonne National Laboratory, Rept. ANL–80–74, 1980.

[8] Powell, M. J. D. “A Fortran Subroutine for Solving Systems of Nonlinear Algebraic Equations.”Numerical Methods for Nonlinear Algebraic Equations, P. Rabinowitz, ed., Ch.7, 1970.

Extended Capabilities

Version History

Introduced before R2006a

expand all