about polyval and polyfit
Show older comments
y=[22.9000
23.2000
23.0000
24.4000
26.7000
25.0000
25.5000
26.3000
26.1000
26.2000
27.0000
26.3000
28.1000
26.4000
27.8000]';
x1=1:15;
x2=11:25;
x3=22:36;
x4=11111:11125;
y1=polyval(polyfit(x1,y,14),x1);
y2=polyval(polyfit(x2,y,14),x2);
y3=polyval(polyfit(x3,y,14),x3);
y4=polyval(polyfit(x4,y,14),x4);
then,why y1,y2,y3,y4 are quite different?
I think,theoretically they are exactly y.
Thanks for any help.
Accepted Answer
More Answers (2)
Wayne King
on 9 Oct 2011
Hi XU,
Keep in mind that polyfit() is fitting the best polynomial in the least-squares sense. You are trying to fit a polynomial with the same y-values but for vastly different values of x, so why do you expect the fit to be exactly the same?
Further, these are badly-conditioned fits. Reduce the order of your polynomial to 3 (for example) and see what happens for y1 to y3. (y4 is still badly conditioned).
y=[22.9000 23.2000 23.0000 24.4000 26.7000 25.0000 25.5000 26.3000 26.1000 26.2000 27.0000 26.3000 28.1000 26.4000 27.8000];
x1=1:15;
x2=11:25;
x3=22:36;
x4=11111:11125;
y1=polyval(polyfit(x1,y,3),x1);
y2=polyval(polyfit(x2,y,3),x2);
y3=polyval(polyfit(x3,y,3),x3);
2 Comments
xu
on 10 Oct 2011
Walter Roberson
on 10 Oct 2011
No, you are not right.
x^14 will have a precision at least 14 bits less than x itself will. You get big errors when you create a 14 degree polynomial for a non-trivial range. You would get higher precision if you created the horner version of the polynomial.
What might be *algebraically* true really doesn't matter, because real floating point arithmetic does not obey the rules of algebra. In floating point arithmetic, there are _many_ values x such that (1+x)-1 evaluates to 0. For example, (1 + 1E-100)-1 is *not* going to be 1E-100.
xu
on 12 Oct 2011
0 votes
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!