MATLAB: Why polyfit poly roots is not equal to primitive poly roots

MATLAB and Simulink Student Suitepolyfit poly roots is not equal to primitive poly roots

x=(-1:0.01:1);
syms w;
p_pie=(w-1.6)*(w+1.2)*(w-1.5)*(w-1.2);
%fitting p=sqrt(2)*p_pie,the roots of p is equal to p_pie
p_pie=sym2poly(p_pie);
r_pie=roots(p_pie)
p=sqrt(2)*polyval(p_pie,x);
p_fit=polyfit(p,x,4);
r=roots(p_fit)
% r_pie =
%

% -1.200000000000000
% 1.599999999999999
% 1.500000000000008
% 1.199999999999997
% r =
%
% -8.611410578949965 + 0.000000000000000i
% 0.893177977748609 + 4.949441426509091i
% 0.893177977748609 - 4.949441426509091i
% -3.860244385178206 + 0.000000000000000i

Best Answer

Because you did not use polyfit correctly. Look carefully at what you did!
p_fit=polyfit(p,x,4);
What are the arguments to polyfit? Look carefully. From the help for polyfit:
polyfit Fit polynomial to data.
P = polyfit(X,Y,N) finds the coefficients of a polynomial P(X) of
degree N that fits the data Y best in a least-squares sense.
What does polyfit expect the FIRST argument will be?
What does it expect to see for the SECOND argument?
Why would you invert the order that you passed in those two arguments? Can MATLAB possibly know what you wanted it to do, even though you told it to do something else? (If I had to guess why you did it that way, it is because of the argument sequence to polyval, which may have gotten you confused with polyfit. But the rule to using ANY tool correctly is to read the help. Look carefully at the argument list, and know what you are passing in.)
Related Question