how to write cubic function as modelfun for nlinfit?
이전 댓글 표시
I have two vectors named X and Y, I want to find 4 parameters by using nlinfit and using following cubic function:

How can I convert this cubic function in form of modelfun ?
댓글 수: 6
John D'Errico
2017년 12월 16일
Are you asking about nlinfit? Why would you, when it is far simpler to just use polyfit?
tehreem fatima
2017년 12월 16일
John D'Errico
2017년 12월 16일
편집: John D'Errico
2017년 12월 16일
What is nlfit? As far as I know, there is no tool in MATLAB with that name. How do you expect help asking about a function that does not apparently exist?
And you still have not said why it is that polyfit is not a far better choice?
polyfit will be more accurate. It will be significantly faster than any nonlinear regression tool.
the cyclist
2017년 12월 16일
I'm going to make a guess here, that you think you need a non-linear fit because have terms like Y^3. But "non-linear" in this context refers to the fitted parameters (which in your case are all linear). So, you are better off using polyfit as John has suggested.
I'm happy to provide an answer using either of those functions if you prefer, but it would be good to clarify why you are making your choices.
tehreem fatima
2017년 12월 16일
the cyclist
2017년 12월 16일
So, my guess was correct.
Your function is not linear in Y. But your function is linear in the parameters (a,b,c,d). That is the requirement for a linear regression. John has shown you an efficient solution for that linear regression.
답변 (1개)
John D'Errico
2017년 12월 16일
편집: John D'Errico
2017년 12월 17일
Again, you only THINK you need to use nlinfit. But you simply don't.
x = rand(1,10);
y = rand(1,10);
P3 = polyfit(x,y,3)
P3 =
-9.8371 13.772 -4.9255 0.78821
Of course, my data is complete garbage, but that is not relevant. USE POLYFIT. The coefficients that are returned are the coefficients of the least squares regression CUBIC polynomial, in descending order of the corresponding term.
help polyfit
Polyfit is faster than nlinfit. It is more accurate.
That a function is nonlinear in the independent variable does not necessarily mean that you need to use a nonlinear regression tool! That is not why nlinfit exists.
Don't use a Mack truck to carry a pea to Boston.
Sometimes people think that a linear regression implies that you need a purely linear model, like y=a*x+b. In fact, the model
y = a*x^3 + b^x^2 + c*x + d
is fittable using a linear regression tool (like polyfit, or regress). There is no need to use a nonlinear regression.
And when you use a nonlinear regression tool to fit a simple model that does not require it, the nonlinear tool will require starting values, will try to compute a numerical estimate of the Jacobian matrix, and will assume it needs to iterate until convergence. All of that is not necessary, if you use a tool like polyfit. So just use the proper tool. Here, that is polyfit.
카테고리
도움말 센터 및 File Exchange에서 Optimization Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!