about polyval and polyfit
6 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Jan
2011-10-9
A solution is described in the appearing warning message: center and scale the data. As the message claims, this is explained in "help polyfit".
y = [22.9, 23.2, 23, 24.4, 26.7, 25, 25.5, 26.3, 26.1, ...
26.2, 27, 26.3, 28.1, 26.4, 27.8]
x1 = 1:15;
p1 = polyfit(x1, y, 14); % ==> Warning appears.
% Let POLYFIT scale and center the data:
[p1, s1, u1] = polyfit(x1, y, 14); % ==> no warning
yy = polyval(p1, x1, s1, u1);
max(abs(y-yy)) % >> 9.2122e-012
The resulting difference is 2500*eps. This is in the expected range, because polynomials of order 14 are extremly sensitive to changes in the points at the edges.
2 个评论
John D'Errico
2011-10-12
No, you are not right! You are forgetting that the arithmetic will be done in floating point, using doubles. Raise 15 to the 14th power. Now, raise 11125 to the 14th power. Both numbers are larger than the largest number that can be represented as a double anyway. But even so, numbers this large will completely destroy the ability to do linear algebra (as polyfit must do) on the arrays it will build. Polynomial models of that order are famously, foolishly poor things to build anyway. Doing so without centering and scaling the data is just criminal abuse of data.
更多回答(2 个)
Wayne King
2011-10-9
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 个评论
Walter Roberson
2011-10-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!