Different polyfit / polyval

7 ビュー (過去 30 日間)
Furkan Kocaman
Furkan Kocaman 2019 年 6 月 22 日
コメント済み: Walter Roberson 2019 年 6 月 22 日
I want to understand the functions polyval and polyfit. At First I programming in Matlab the following lines:
cooefs = [-15.3800,74.8008, -53.3701, 346.3550, 0]
x = (0:.01:95)/180*pi;
y = polyval(cooefs(end:-1:1), x);
[p,~,mu] = polyfit(y, x, 5);
My first question is: The result from polyfit is not the same if I write this line:
p = polyfit(y, x, 5);
Why? The Variable p must be the same. The next question is, are any possibility to convert an matlab script with polyval / polyfit to C or python. The Matlab coder cant do this.

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 6 月 22 日
The two results do not need to be same. When you use the version that outputs mu then you would pass mu in to polyval.
The version with mu does a linear shift and scaling.
polyfit without the scaling can be written in terms of constructing a vandermode matrix and using the \ operator. Those can have code generation.
  2 件のコメント
Furkan Kocaman
Furkan Kocaman 2019 年 6 月 22 日
And how can I use the scaling in Python or in C?
Walter Roberson
Walter Roberson 2019 年 6 月 22 日
The help documentation phrases it as
[P,S,MU] = polyfit(X,Y,N) finds the coefficients of a polynomial in
XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). This
centering and scaling transformation improves the numerical properties
of both the polynomial and the fitting algorithm.
This should provide sufficient clue as to how to create XHAT yourself.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by