about polyval and polyfit

6 visualizzazioni (ultimi 30 giorni)
xu
xu il 9 Ott 2011
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.

Risposta accettata

Jan
Jan il 9 Ott 2011
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 Commenti
xu
xu il 10 Ott 2011
Thanks for your reply.I know what you mean.But my question is, if I fit y (there are 15 points in it)with a polynomial p(x) of degree 14----then there are 15 undetermined coefficients,and each pair(xi,yi) can generate one equation,so finally we have 15 equations and 15 unknowns.I think the coefficients of p(x) can be determined through solving the 15 equations.Consequently,if I evaluate p(x) at x,the result should be the same with y.Am I right? Thanks.
John D'Errico
John D'Errico il 12 Ott 2011
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.

Accedi per commentare.

Più risposte (2)

Wayne King
Wayne King il 9 Ott 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 Commenti
xu
xu il 10 Ott 2011
Thanks for your reply.I know what you mean.But my question is, if I fit y (there are 15 points in it)with a polynomial p(x) of degree 14----then there are 15 undetermined coefficients,and each pair(xi,yi) can generate one equation,so finally we have 15 equations and 15 unknowns.I think the coefficients of p(x) can be determined through solving the 15 equations.Consequently,if I evaluate p(x) at x,the result should be the same with y.Am I right? Thanks.
Walter Roberson
Walter Roberson il 10 Ott 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.

Accedi per commentare.


xu
xu il 12 Ott 2011
polyfit by this way,i.e.[p1, s1, u1] = polyfit(x1, y, 14),is more satisfactory when x is not so regular.But I don't know why.

Categorie

Scopri di più su Polynomials in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by