Polyfit isn't returning linear gradient and intercept
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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?
採用された回答
dpb
2025 年 8 月 3 日
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 件のコメント
Jolene
2025 年 8 月 3 日
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
2025 年 8 月 3 日
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.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Lengths and Angles についてさらに検索
タグ
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
