Clear Filters
Clear Filters

Numerical fit in Matlab

5 views (last 30 days)
Andoni Mendialdua
Andoni Mendialdua on 13 Aug 2014
Commented: Star Strider on 13 Aug 2014
Hi all, I have the next equation: QE= s * T1 - s * a * T2 + s * e * T3 + r ;
where I know the values of QE for different values of T1, T2 and T3
and s,a,e and r are some constants that I need to find in order to fit the equation in the best way.
What I would like to know is if there is any function in Matlab that will help me to find these constants, for fitting the equation.
I would be glad if someone can help me with this problem.
Thank you very much,
Andoni

Accepted Answer

Star Strider
Star Strider on 13 Aug 2014
Edited: Star Strider on 13 Aug 2014
It is nonlinear by definition, so you would need to use the Statistics Toolbox function nlinfit or the Optimization Toolbox function lsqcurvefit.
You could also use the core MATLAB function fminsearch, with an additional line or two of code.
  9 Comments
John D'Errico
John D'Errico on 13 Aug 2014
Edited: John D'Errico on 13 Aug 2014
In fact, the parameter variances for the original model CAN INDEED be estimated from the transformed case, and that is not much more work than it takes to estimate them for the nonlinear model. Any of those parameter variances are simply approximations anyway. Don't pretend that the EXACT, TRUE variances can be obtained, or that those variances CAN be estimated "uniquely". The word uniquely here is meaningless in that context.
Anyway, the poster NEVER stated anything about wanting parameter variances for those estimates.
Finally, telling them to use fminsearch for a problem like this is even sillier, to use a slowly convergent optimizer, that will yield only a few digits of accuracy, IF it converges at all. Why tell a person to use fminsearch, forcing them to learn how to first create the sum of squares objective, then requiring them to learn about starting values, iteration counts, tolerances, etc?
By the way, fminsearch does not return parameter variances either, so your argument fails there completely. While parameter variances CAN be computed for such a problem, that would require a moderate amount of knowledge about how to do so, and the pitfalls inherent in those computations.
Star Strider
Star Strider on 13 Aug 2014
I suggested fminsearch because not everyone has the Statistics or Optimization (or Curve Fitting) Toolboxes.
I stand by my assertion that the function is nonlinear in the parameters, and therefore a nonlinear solver is appropriate for it.
That’s all I have to say on this particular Question.

Sign in to comment.

More Answers (2)

John D'Errico
John D'Errico on 13 Aug 2014
The presumed model is
QE = s * T1 - s * a * T2 + s * e * T3 + r
where data is provided in the form of (T1,T2,T3,QE) quadruplets, and s,e,a,r are all unknowns to be estimated using LINEAR regression analysis.
I'll assume that the data is sufficient to estimate 4 parameters. Yeah, I know, but SOOOOOOO often I see people try to fit a model without adequate data to fit the model. How much data do you need? The simple answer is that you always need more data than you want to provide. As important is where the data points lie in the (T1,T2,T3) space.
The important point is that this is NOT truly a nonlinear model. Transform it so that
u = -s*a
v = s*e
Note that I put the minus sign into the transformation for u.
Then your model becomes
QE = s*T1 + u*T2 + v*T3 + r
Clearly this model is linear in the parameters s,u,v,r.
You can estimate the parameters using regress from the stats toolbox. Or you can use my own polyfitn from the File Exchange. You can also use just backslash.
So assuming that T1, T2, T3, T4, QE are all vectors of the same length, do this...
suvr = [T1(:),T2(:),T3(:),ones(numel(T1),1)]\QE(:);
I put the colons in there so I won't need to worry about whether your vectors are row or column vectors. suvr will be a vector of size 4x1, containing the values of each parameter. As long as s is not identically zero, you can now recover e and a.
s = suvr(1);
r = suvr(4);
a = -suvr(2)./s;
e = suvr(3)./s;
See that the transformation I used requires only that s was not zero. Of course, if it was, then your model becomes a rather trivial one anyway.
  2 Comments
Andoni Mendialdua
Andoni Mendialdua on 13 Aug 2014
Thank you very much John D´Errico. My question may seem stupid, but what if I use nlinfit function for a linear model?
John D'Errico
John D'Errico on 13 Aug 2014
Edited: John D'Errico on 13 Aug 2014
You have no need to use nlinfit for this.
The point is, nlinfit needs a starting value. It is an iterative scheme, that will need to converge. If your starting values are poor, then it may not converge well, or even at all. The iterative scheme will only converge to within your tolerances, so you need to worry about those convergence tolerances. Using a nonlinear scheme for a linear model is silly in the extreme, wild overkill.
If you have nlinfit, then use regress instead.

Sign in to comment.


Andoni Mendialdua
Andoni Mendialdua on 13 Aug 2014
And btw I dont understand very well the part is in bold here: suvr = [T1(:),T2(:),T3(:), ones(numel(T1),1)]\QE(:) ;
Thank you,
Andoni
  1 Comment
John D'Errico
John D'Errico on 13 Aug 2014
DON'T add an answer when you have a question about another answer! Ask a question about an answer as a comment on that answer!
The vector of ones simply adds a constant term into the model, i.e., the r term.
The \QE part is how backslash works.
help slash
backslash solves the LINEAR regression problem
A*x = y
where A is a matrix, as
x = A\y
Here y is the dependent variable, just as QE is for your case.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!