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

 채택된 답변

Star Strider
Star Strider 2014년 8월 13일
편집: Star Strider 2014년 8월 13일

1 개 추천

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

Star Strider
Star Strider 2014년 8월 13일
Andoni’s ‘Answer’ moved here:
Thank you very much Star Strider for your fast answer. Do you know which is the difference between nlinfit and lsqcurvefit?
Thank you,
Andoni
Star Strider
Star Strider 2014년 8월 13일
They are very similar in their behaviour and results. The most obvious differences are that you can get more statistics from nlinfit, and you can constrain the parameters to specific limits in lsqcurvefit (that also seems to be a bit more robust). The core MATLAB function fminsearch is much like nlinfit but it requires one extra line of code (an anonymous function to compute the squared-error) to work for curve fitting.
Andoni Mendialdua
Andoni Mendialdua 2014년 8월 13일
편집: Andoni Mendialdua 2014년 8월 13일
Thank you very much Star Strider for your fast answer. I have been informing about nlinfit function and I have a doubt. nlinfit function has the next form beta = nlinfit(X,Y,modelfun,beta0), where, if I am not understanding bad, x and y are the input values. In my case, I have 4 input values (QE, T1, T2 and T3), so how do I use this function?
Thank you very much,
Andoni
My pleasure!
You would need to combine the T1, T2, T3 vectors in a matrix:
TM = [TE1 TE2 TE3];
This assumes they are all column vectors. (Transpose them to column vectors otherwise.) QE is your independent variable, so it is a column vector.
Your parameters, s, a, e, r also become one parameter vector, in this instance b(1) ... b(4) respectively. As an anonymous function, your equation to fit (in both nlinfit and lsqcurvefit, and indirectly with fminsearch) becomes:
QEfun = @(b,TM) b(1) .* TM(:,1) - b(1) .* b(2) .* TM(:,2) + b(1) .* b(3) .* TM(:,3) + b(4);
I ‘vectorised’ it (note the ‘.*’ instead of ‘*’) so it will compute element-by-element values, required in the curve-fitting functions.
The exact syntax for nlinfit and lsqcurvefit varies, with b0 your initial estimate for your parameters in both:
B = nlinfit(TM, QE, QEfun, b0)
B = lsqcurvefit(QEfun, b0, TM, QE)
Explore the other options for both functions. They offer a variety of input and output possibilities, depending on what you want to do.
John D'Errico
John D'Errico 2014년 8월 13일
NO, NO, NO. There is absolutely NO need for a nonlinear fit here!
This is intrinsically a linear model, since it can be trivially reparameterized to be a linear model, even without a change to the error structure. I'll post an answer that explains how to do so.
Andoni Mendialdua
Andoni Mendialdua 2014년 8월 13일
I do not know how I can thank you for this!
Andoni
Star Strider
Star Strider 2014년 8월 13일
편집: Star Strider 2014년 8월 13일
My pleasure!
Your function is by definition nonlinear in the parameters (the partial derivatives of the function with respect to a, s, and e are functions of other parameters), so while you can force it to be linearised, the parameter variances cannot be uniquely estimated unless you use a nonlinear fit.
John D'Errico
John D'Errico 2014년 8월 13일
편집: John D'Errico 2014년 8월 13일
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 2014년 8월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

John D'Errico
John D'Errico 2014년 8월 13일

0 개 추천

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

Andoni Mendialdua
Andoni Mendialdua 2014년 8월 13일
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 2014년 8월 13일
편집: John D'Errico 2014년 8월 13일
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
Andoni Mendialdua 2014년 8월 13일

0 개 추천

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

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

태그

질문:

2014년 8월 13일

댓글:

2014년 8월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by