Some confusion on polyfit?

Greetings all,
This one is throwing me for a loop. So I have some data that I was preforming a linear fit on with the basic fit tool GUI, and I receive the coefficients
Coefficients:
p1 = -0.0068338
p2 = 3.9156
Cool - so I wanted to double verify if these numbers were good, so I used the polyfit command:
[fitResults1,S1,mu1] = polyfit(xdata1,ydata1,1)
but I get different coefficients:
-0.1138 2.4694
So, I generated code from the plot to show me what command they are using to get the first sent of coefficients, and they are using polyfit like me, except it's of the form:
fitResults1 = polyfit(xdata1, ydata1, 1)
Notice it's not in brackets as described in http://www.mathworks.com/help/matlab/ref/polyfit.html
but why are the coefficients so different between the two? I double checked everything (i.e., if xdata1 was the x data from my plot and ydata1 was the y data from my plot, and it was).
Did I stumble on a bug?
Thanks!

 Accepted Answer

Not a bug at all.
When you used:
[fitResults1,S1,mu1] = polyfit(xdata1,ydata1,1)
you invoked the centreing and scaling of x that polyfit does if you ask for the mu output. You’re fitting different x-data.

8 Comments

Oh dang really? Would that be the same as centering as available on the basic fitting tool GUI?
I don’t have the Curve Fitting Toolbox (redundant for me, with the Statistics and Optimization Toolboxes) so I don’t know how it works. It seems from the code you posted that the Curve Fitting Toolbox invokes polyfit without the centreing and scaling. I can only assure you that centreing and scaling produces different parameters because the x-data are different than an un-scaled fit.
Thanks for the clarification!
My pleasure!
Star,
Sorry to keep this thread open, but just one more question - is there a way to not center and scale x when you want S1 and mu1 returned?
Thanks!
Jesse
Star Strider
Star Strider on 21 Aug 2014
Edited: Star Strider on 21 Aug 2014
No worries! Keeping this thread open is preferable to starting a new one on the same topic.
Asking for only S1 will not centre and scale, but will give you important information (for instance if you want to use polyparci). If you want the mu vector without centreing and scaling, since according to the documentation µ1=mean(x) and µ2=std(x), you can simply calculate them yourself.
Ok cool thanks Star! I will try that!
My pleasure!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 21 Aug 2014

Commented:

on 21 Aug 2014

Community Treasure Hunt

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

Start Hunting!