how to write cubic function as modelfun for nlinfit?
Show older comments
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 Comments
John D'Errico
on 16 Dec 2017
Are you asking about nlinfit? Why would you, when it is far simpler to just use polyfit?
tehreem fatima
on 16 Dec 2017
John D'Errico
on 16 Dec 2017
Edited: John D'Errico
on 16 Dec 2017
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
on 16 Dec 2017
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
on 16 Dec 2017
the cyclist
on 16 Dec 2017
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.
Answers (1)
John D'Errico
on 16 Dec 2017
Edited: John D'Errico
on 17 Dec 2017
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.
Categories
Find more on Optimization 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!