Model fit using lsqcurvefit (non-linear least squares fitting)
Show older comments
Hi,
I've been trying to use lsqcurvefit for a simple equation: y = a*x(1) + b*x(2) + c*x(3), where a,b and c are the unknowns (constants) and I have the vectors y,x(1),x(2) and x(3).
For now, I have a reference for 'a' so I know if the values I get from the model are more or less correct.
When I tried linear least squares functions the results were very unstable and 'a' can get almost every value depending on the value of 'c'.
However, using lsqcurvefit I got putty good estimations of 'a' but I still have stability/sensitivity issues depending on initial conditions and boundries.
Does anyone know what are the best assigned properties for lsqcurvefit to solve such a model?
Also, I don't understand why lsqcurvefit yield better results than the simpler linear fitting functions.
Are there better alternatives? 'fminsearch' for example or other?
Thanks !
3 Comments
Zack
on 27 Oct 2012
Matt J
on 28 Oct 2012
I can't explain why lsqcurvefit does better, based on your comments so far, but it sounds like you are doing ordinary least squares instead of total least squares fitting. The latter is better when you have noise in x. The code in my answer below implements it, so you might want to give it a try.
Answers (2)
Star Strider
on 27 Oct 2012
Edited: Star Strider
on 27 Oct 2012
'When I tried linear least squares functions the results were very unstable and 'a' can get almost every value depending on the value of 'c'.'
This suggests to me that at least x(1) and x(3) and perhaps y could be highly correlated. You may want to explore these possibilities with corrcoef and related functions. See what the results of:
[R,P] = corrcoef([x(1) x(2) x(3)]);
are. (I assume here that x(1), x(2), and x(3) are column vectors.) Since the R values may be difficult to interpret, look at the P values. A value of P <= 0.05 means that the corresponding colums of x are highly correlated.
4 Comments
Star Strider
on 27 Oct 2012
Edited: Star Strider
on 27 Oct 2012
You might want to consider experimenting with the various tolerances that Optimization Options offers.
One thing I didn't ask about previously was the possiblity that you could have scaling problems — some of your variables could be several orders of magnitude different from others. This frequently causes problems in linear and nonlinear regression because it challenges machine precision limits. The solution in that situation is relatively easy, since your function is linear — normalize your x vectors (multiplying them by individual scale factors) to roughly the same orders of magnitude (preferably to within about 100 times each other or less), then rescale your estimated parameters by those factors when your regression has converged. The nonlinear parameter estimation functions are relatively more robust to scaling problems than the linear parameter estimation functions are, so lsqcurvefit might have worked when the linear functions failed.
If scaling isn't the problem, I suggest that you explore the possibility that your variables could be correlated.
Zack
on 29 Oct 2012
Star Strider
on 29 Oct 2012
If they're correlated, the columns are essentially linearly dependent (or close to it). It is then difficult to uniquely determine the coefficients. The problem arises in a statement like B = X\Y that is essentially equivalent to B = inv(X)*Y (for illustration only — please don't use inv). In this situation, X with correlated columns is singular or close to it. That could explain your inability to uniquely estimate some of your coefficients.
Matt J
on 27 Oct 2012
function [x0, a, d, normd] = lsplane(X)
% ---------------------------------------------------------------------
% LSPLANE.M Least-squares plane (orthogonal distance
% regression).
%
% Version 1.0
% Last amended I M Smith 27 May 2002.
% Created I M Smith 08 Mar 2002
% ---------------------------------------------------------------------
% Input
% X Array [x y z] where x = vector of x-coordinates,
% y = vector of y-coordinates and z = vector of
% z-coordinates.
% Dimension: m x 3.
%
% Output
% x0 Centroid of the data = point on the best-fit plane.
% Dimension: 3 x 1.
%
% a Direction cosines of the normal to the best-fit
% plane.
% Dimension: 3 x 1.
%
% <Optional...
% d Residuals.
% Dimension: m x 1.
%
% normd Norm of residual errors.
% Dimension: 1 x 1.
% ...>
%
% [x0, a <, d, normd >] = lsplane(X)
% ---------------------------------------------------------------------
% check number of data points
m = size(X, 1);
if m < 3
error('At least 3 data points required: ' )
end
%
% calculate centroid
x0 = mean(X)';
%
% form matrix A of translated points
A = [(X(:, 1) - x0(1)) (X(:, 2) - x0(2)) (X(:, 3) - x0(3))];
%
% calculate the SVD of A
[U, S, V] = svd(A, 0);
%
% find the smallest singular value in S and extract from V the
% corresponding right singular vector
[s, i] = min(diag(S));
a = V(:, i);
%
% calculate residual distances, if required
if nargout > 2
d = U(:, i)*s;
normd = norm(d);
end
5 Comments
Zack
on 29 Oct 2012
Zack
on 30 Oct 2012
Star Strider
on 30 Oct 2012
As I understand it, the linear least squares solvers use simple matrix division to calculate the parameters (although they do it in a linear least squares sense). The lsqcurvefit and other nonlinear parameter estimation routines use an interative gradient descent algorithm, calculating the Jacobian at each step. There may be enough variation in your x(1), x(2), and x(3) vectors to allow the nonlinear parameter estimation algorithms to uniquely estimate your parameters. If you have the Statistics Toolbox, it might be interesting to use nlparci to see what the confidence intervals are on the parameters lsqcurvefit estimates, and whether they include zero. You might also see what nlinfit gives. It uses the Levenberg-Marquardt algorithm. That option is also available in lsqcurvefit.
Matt J
on 30 Oct 2012
What unique features of LSQCURVEFIT allow it to overcome collinearity? Or, am I just getting the right value of 'a' with lsqcurvefit by chance because I happened to pick a lucky initial condition?
Yes, it's just a matter of luck. There are obviously infinite solutions if your x1,x2,x3 are linearly dependent. If you initialize at any of these solutions themselves, any normal solver will refuse to progress and offer that point as the solution.
Ahmed Elsherif
on 19 Feb 2020
Hi,
By using
[x,resnorm,~,exitflag,output] = lsqcurvefit(f,x0,E,F3g)
How to set limits for the independent variable , E? I need to do the fitting from E=0 to E=17.
Thanks in advance
Categories
Find more on Choose a Solver in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!