nonlinear data-凯发k8网页登录
the general advice for least-squares problem setup is to formulate the problem in a way that allows solve
to recognize that the problem has a least-squares form. when you do that, solve
internally calls lsqnonlin
, which is efficient at solving least-squares problems. see .
this example shows the efficiency of a least-squares solver by comparing the performance of lsqnonlin
with that of fminunc
on the same problem. additionally, the example shows added benefits that you can obtain by explicitly recognizing and handling separately the linear parts of a problem.
problem setup
consider the following data:
data = ...
[0.0000 5.8955
0.1000 3.5639
0.2000 2.5173
0.3000 1.9790
0.4000 1.8990
0.5000 1.3938
0.6000 1.1359
0.7000 1.0096
0.8000 1.0343
0.9000 0.8435
1.0000 0.6856
1.1000 0.6100
1.2000 0.5392
1.3000 0.3946
1.4000 0.3903
1.5000 0.5474
1.6000 0.3459
1.7000 0.1370
1.8000 0.2211
1.9000 0.1704
2.0000 0.2636];
plot the data points.
t = data(:,1); y = data(:,2); plot(t,y,'ro') title('data points')
the problem is to fit the function
y = c(1)*exp(-lam(1)*t) c(2)*exp(-lam(2)*t)
to the data.
solution approach using default solver
to begin, define optimization variables corresponding to the equation.
c = optimvar('c',2); lam = optimvar('lam',2);
arbitrarily set the initial point x0
as follows: c(1) = 1
, c(2) = 1
, lam(1) = 1
, and lam(2) = 0
:
x0.c = [1,1]; x0.lam = [1,0];
create a function that computes the value of the response at times t
when the parameters are c
and lam
.
diffun = c(1)*exp(-lam(1)*t) c(2)*exp(-lam(2)*t);
convert diffun
to an optimization expression that sums the squares of the differences between the function and the data y
.
diffexpr = sum((diffun - y).^2);
create an optimization problem having diffexpr
as the objective function.
ssqprob = optimproblem('objective',diffexpr);
solve the problem using the default solver.
[sol,fval,exitflag,output] = solve(ssqprob,x0)
solving problem using lsqnonlin. 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.
sol = struct with fields:
c: [2x1 double]
lam: [2x1 double]
fval = 0.1477
exitflag = functionchangebelowtolerance
output = struct with fields:
firstorderopt: 7.8870e-06
iterations: 6
funccount: 7
cgiterations: 0
algorithm: 'trust-region-reflective'
stepsize: 0.0096
message: 'local minimum possible....'
bestfeasible: []
constrviolation: []
objectivederivative: "forward-ad"
constraintderivative: "auto"
solver: 'lsqnonlin'
plot the resulting curve based on the returned solution values sol.c
and sol.lam
.
resp = evaluate(diffun,sol); hold on plot(t,resp) hold off
the fit looks to be as good as possible.
solution approach using fminunc
to solve the problem using the fminunc
solver, set the 'solver'
option to 'fminunc'
when calling solve
.
[xunc,fvalunc,exitflagunc,outputunc] = solve(ssqprob,x0,'solver',"fminunc")
solving problem using fminunc. local minimum found. optimization completed because the size of the gradient is less than the value of the optimality tolerance.
xunc = struct with fields:
c: [2x1 double]
lam: [2x1 double]
fvalunc = 0.1477
exitflagunc = optimalsolution
outputunc = struct with fields:
iterations: 30
funccount: 37
stepsize: 0.0017
lssteplength: 1
firstorderopt: 2.9454e-05
algorithm: 'quasi-newton'
message: 'local minimum found....'
objectivederivative: "forward-ad"
solver: 'fminunc'
notice that fminunc
found the same solution as lsqcurvefit
, but took many more function evaluations to do so. the parameters for fminunc
are in the opposite order as those for lsqcurvefit
; the larger lam
is lam(2)
, not lam(1)
. this is not surprising, the order of variables is arbitrary.
fprintf(['there were %d iterations using fminunc,' ... ' and %d using lsqcurvefit.\n'], ... outputunc.iterations,output.iterations)
there were 30 iterations using fminunc, and 6 using lsqcurvefit.
fprintf(['there were %d function evaluations using fminunc,' ... ' and %d using lsqcurvefit.'], ... outputunc.funccount,output.funccount)
there were 37 function evaluations using fminunc, and 7 using lsqcurvefit.
splitting the linear and nonlinear problems
notice that the fitting problem is linear in the parameters c(1)
and c(2)
. this means for any values of lam(1)
and lam(2)
, you can use the backslash operator to find the values of c(1)
and c(2)
that solve the least-squares problem.
rework the problem as a two-dimensional problem, searching for the best values of lam(1)
and lam(2)
. the values of c(1)
and c(2)
are calculated at each step using the backslash operator as described above. to do so, use the fitvector
function, which performs the backslash operation to obtain c(1)
and c(2)
at each solver iteration.
type fitvector
function yest = fitvector(lam,xdata,ydata) %fitvector used by datdemo to return value of fitting function. % yest = fitvector(lam,xdata) returns the value of the fitting function, y % (defined below), at the data points xdata with parameters set to lam. % yest is returned as a n-by-1 column vector, where n is the number of % data points. % % fitvector assumes the fitting function, y, takes the form % % y = c(1)*exp(-lam(1)*t) ... c(n)*exp(-lam(n)*t) % % with n linear parameters c, and n nonlinear parameters lam. % % to solve for the linear parameters c, we build a matrix a % where the j-th column of a is exp(-lam(j)*xdata) (xdata is a vector). % then we solve a*c = ydata for the linear least-squares solution c, % where ydata is the observed values of y. a = zeros(length(xdata),length(lam)); % build a matrix for j = 1:length(lam) a(:,j) = exp(-lam(j)*xdata); end c = a\ydata; % solve a*c = y for linear parameters c yest = a*c; % return the estimated response based on c
solve the problem using solve
starting from a two-dimensional initial point x02.lam
= [1,0]
. to do so, first convert the fitvector
function to an optimization expression using fcn2optimexpr
. see . to avoid a warning, give the output size of the resulting expression. create a new optimization problem with objective as the sum of squared differences between the converted fitvector
function and the data y
.
x02.lam = x0.lam; f2 = fcn2optimexpr(@(x) fitvector(x,t,y),lam,'outputsize',[length(t),1]); ssqprob2 = optimproblem('objective',sum((f2 - y).^2)); [sol2,fval2,exitflag2,output2] = solve(ssqprob2,x02)
solving problem using lsqnonlin. 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.
sol2 = struct with fields:
lam: [2x1 double]
fval2 = 0.1477
exitflag2 = functionchangebelowtolerance
output2 = struct with fields:
firstorderopt: 4.4032e-06
iterations: 10
funccount: 33
cgiterations: 0
algorithm: 'trust-region-reflective'
stepsize: 0.0080
message: 'local minimum possible....'
bestfeasible: []
constrviolation: []
objectivederivative: "finite-differences"
constraintderivative: "auto"
solver: 'lsqnonlin'
the efficiency of the two-dimensional solution is similar to that of the four-dimensional solution:
fprintf(['there were %d function evaluations using the 2-d ' ... 'formulation, and %d using the 4-d formulation.'], ... output2.funccount,output.funccount)
there were 33 function evaluations using the 2-d formulation, and 7 using the 4-d formulation.
split problem is more robust to initial guess
choosing a bad starting point for the original four-parameter problem leads to a local solution that is not global. choosing a starting point with the same bad lam(1)
and lam(2)
values for the split two-parameter problem leads to the global solution. to show this, rerun the original problem with a start point that leads to a relatively bad local solution, and compare the resulting fit with the global solution.
x0bad.c = [5 1]; x0bad.lam = [1 0]; [solbad,fvalbad,exitflagbad,outputbad] = solve(ssqprob,x0bad)
solving problem using lsqnonlin. 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.
solbad = struct with fields:
c: [2x1 double]
lam: [2x1 double]
fvalbad = 2.2173
exitflagbad = functionchangebelowtolerance
outputbad = struct with fields:
firstorderopt: 0.0036
iterations: 31
funccount: 32
cgiterations: 0
algorithm: 'trust-region-reflective'
stepsize: 0.0012
message: 'local minimum possible....'
bestfeasible: []
constrviolation: []
objectivederivative: "forward-ad"
constraintderivative: "auto"
solver: 'lsqnonlin'
respbad = evaluate(diffun,solbad); hold on plot(t,respbad,'g') legend('data','global fit','bad local fit','location','ne') hold off
fprintf(['the residual norm at the good ending point is %f,' ... ' and the residual norm at the bad ending point is %f.'], ... fval,fvalbad)
the residual norm at the good ending point is 0.147723, and the residual norm at the bad ending point is 2.217300.
see also
|