Numerical fit in Matlab
Show older comments
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
More Answers (2)
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
on 13 Aug 2014
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.
Andoni Mendialdua
on 13 Aug 2014
0 votes
1 Comment
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.
Categories
Find more on Get Started with Curve Fitting Toolbox 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!