Why do the fitting coefficients for polyfit change depending on the number of outputs?

6 views (last 30 days)
Curious to why the fitting coefficients produced by polyfit() change depending on the number of outputs requested. For example,
This produces,
x = 1:100;
y = -0.3*x + 2*randn(1,100);
[p,S] = polyfit(x,y,1);
p =
-0.3050 0.0635
While this produces,
[p,S,mu] = polyfit(x,y,1);
p =
-8.8479 -15.3380
Thanks for the feedback

Accepted Answer

Steven Lord
Steven Lord on 17 Jul 2019
Edited: Steven Lord on 17 Jul 2019
See the third item in the Description section of the polyfit documentation page. That third output tells MATLAB to center and scale your data. That centered and scaled data is used in place of the "raw" x data to create the polynomial fit.
As stated in the description of the mu output argument on that documentation page, if you ask for the third output from polyfit you'll need to specify that output as an input when or if you call polyval. This will tell polyval how to transform your data before evaluating the fit.
Looking at the data from the "Use Centering and Scaling to Improve Numerical Properties" on that documentation page, would you rather work with data on the order of 3.2e16 to fit a fifth degree polynomial or data on the order of 8?
>> x = 1750:25:2000;
>> max(x.^5)
ans =
3.2000e+16
>> xnorm = normalize(x, 'zscore');
>> max(xnorm.^5)
ans =
7.7870

More Answers (0)

Community Treasure Hunt

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

Start Hunting!