Different polyfit / polyval
8 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Walter Roberson
on 22 Jun 2019
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 Comments
Walter Roberson
on 22 Jun 2019
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!