I get the wrong polyfit

5 views (last 30 days)
jiang tao
jiang tao on 11 May 2018
Commented: Walter Roberson on 11 May 2018
ftz = [1.4846i 1.1582i];
P_pie=poly(ftz);%roots=ftz
for j=-2:0.01:2
k=floor(((j+2)/0.01)+1);
P_pie_subs(k)=polyval(P_pie,j*1i);
P_subs(k)=P_pie_subs(k)/(2*sqrt(2));
end
x=(-2:0.01:2);
x=x*1i;
P=polyfit(x,P_subs,2);
roots(P)
% ans =
% 0.115192576001519 + 1.337122816059961i
% -0.115192576001520 + 1.337122816059962i

Accepted Answer

Walter Roberson
Walter Roberson on 11 May 2018
ftz = [1.4846i 1.1582i];
P_pie=poly(ftz);%roots=ftz
jvals = -2:0.01:2;
for jidx = 1 : length(jvals)
j = jvals(jidx);
P_pie_subs(jidx)=polyval(P_pie,j*1i);
P_subs(jidx)=P_pie_subs(jidx)/(2*sqrt(2));
end
x=(-2:0.01:2);
x=x*1i;
P=polyfit(x,P_subs,2);
roots(P)
You forgot to take into account that binary floating point does not have an exact representation of 0.01, so your j values might not be exact multiples of 0.01 and floor() might get you a different index than you expect.
  2 Comments
jiang tao
jiang tao on 11 May 2018
Oh!So this is ah,it's wrong to define k. Thank you Mr.Walter Roberson for solving my puzzle.
Walter Roberson
Walter Roberson on 11 May 2018
If you had used round() instead of floor() you probably would have gotten what you wanted.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!