Model fit using lsqcurvefit (non-linear least squares fitting)

11 views (last 30 days)
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
Zack on 27 Oct 2012
1. Both has measurement noise. x(2) and x(3) has the largest noise. y and x(1) are much less noisy.
2. Using regress,lsqlin,etc.. You mean there might be an error in the code?
3. If I put a number instead of 'c'.
4. so why do I get good results using lsqcurvefit ? This is true for ~10 different sets of data.
Matt J
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.

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 27 Oct 2012
Edited: Star Strider on 27 Oct 2012
The only suggestion I have is to explore the possibilities of optimset.
'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
Zack
Zack on 29 Oct 2012
Hi,
Thank you for your answer ! I don't understand why is it a problem if the variables (x(1) and x(3) for example) are correlated.
Star Strider
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.

Sign in to comment.


Matt J
Matt J on 27 Oct 2012
See if this does what you need. It's from a recent Newsgroup thread
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
Matt J
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
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

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!