Different polyfit / polyval

조회 수: 9 (최근 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 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