Different polyfit / polyval

6 次查看(过去 30 天)
Furkan Kocaman
Furkan Kocaman 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 CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by