Polyfit isn't returning linear gradient and intercept
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
testx = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
testx = 1×10
1 2 3 4 5 6 7 8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
testy =[4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
testy = 1×10
4 6 8 10 12 14 16 18 20 22
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[PolyCoeff, S, mu] = polyfit(testx, testy, 1)
PolyCoeff = 1×2
6.0553 13.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = struct with fields:
R: [2×2 double]
df: 8
normr: 7.9936e-15
rsquared: 1
mu = 2×1
5.5000
3.0277
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So my question is - why isn't PolyCoeff returning (1) as 2, and (2) as 2? Where are 6 and 13 coming from? am I missing something really silly and obvious?
Accepted Answer
dpb
on 3 Aug 2025
testx = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
testx = 1×10
1 2 3 4 5 6 7 8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
testy =[4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
testy = 1×10
4 6 8 10 12 14 16 18 20 22
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[PolyCoeff] = polyfit(testx, testy, 1)
PolyCoeff = 1×2
2.0000 2.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What you're missing is that according to the documentation for polyfit, when you ask for the optional third output of the mean,
4 Comments
Jolene
on 3 Aug 2025
Thank you for your answer, I realised shortly after I posted (Sod's law) that the third output argument was causing the issue... however, I don't see why calling this third argument changes the values of the polynomial coefficients, why would it do that?
I see that the documentation states that x is rescaled and normalised to zero, I guess I didn't realise that meant it would also retroactively change the coefficient output. I'll go back through my scripts and remove the mu argument.
Thanks again, appreciate you taking the time for my silly question! I will have to read and understand documentation more closely in future! I took that it was such a basic function for granted!
testx = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
testy = [4, 6, 8, 10, 12, 14, 16, 18, 20, 22];
m = mean(testx)
m = 5.5000
s = std(testx)
s = 3.0277
[PolyCoeff, S, mu] = polyfit(testx, testy, 1)
PolyCoeff = 1×2
6.0553 13.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = struct with fields:
R: [2×2 double]
df: 8
normr: 7.9936e-15
rsquared: 1
mu = 2×1
5.5000
3.0277
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
PolyCoeff(1) * (testx - mu(1)) / mu(2) + PolyCoeff(2)
ans = 1×10
4.0000 6.0000 8.0000 10.0000 12.0000 14.0000 16.0000 18.0000 20.0000 22.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
testy
testy = 1×10
4 6 8 10 12 14 16 18 20 22
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
●
%Recover coefficients of PolyCoeff(1) * testx + PolyCoeff(2)
PolyCoeff(1)/mu(2)
ans = 2.0000
PolyCoeff(1)*(-mu(1))/mu(2) + PolyCoeff(2)
ans = 2.0000
Jolene
on 3 Aug 2025
This is really clear, thanks so much for taking the time.
"...I didn't realise that meant it would also retroactively change the coefficient output. "
testx = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
testy = [4, 6, 8, 10, 12, 14, 16, 18, 20, 22];
testz = zscore(testx) % standardize the independent variable
testz = 1×10
-1.4863 -1.1560 -0.8257 -0.4954 -0.1651 0.1651 0.4954 0.8257 1.1560 1.4863
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[mean(testz) std(testz)]
ans = 1×2
0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
and, indeed, as advertised mean, std deviation.
Now,
polyfit(testz,testy,1) % fit vs z instead of vs x
ans = 1×2
6.0553 13.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
and you see you get the coefficients returned by polyfit in the three-output case. It is the same as if the coefficients were scaled after fitting y vs x but polyfit actually uses the z-score values as the independent variable to improve numerical precision for higher-order polynomials, This is shown in the Alogorithms section of the documentation.
"I will have to read and understand documentation more closely in future! I took that it was such a basic function for granted!"
<LOL> Yes, Mathworks works very diligently to ensure that computations are as accurate as possible so what seems trivial on the surface may have very in-dpeth implementation consequences.
In the case of polyfit being one of the historical functions that has been included since the very early days, its implementation if beginning today would be unlikely to have this behavior at the user level of changing the algorithm based on the expected return variable(s) but would be implemented with named parameter pairs to control the behavior. But, it remains as is for backward compatibility.
More Answers (0)
Categories
Find more on Reinforcement Learning in Help Center and File Exchange
Tags
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)