Warning says polynomial is badly conditioned, but it seems to fit with the data
조회 수: 111 (최근 30일)
이전 댓글 표시
When using polyfit, I get the following error:
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP
POLYFIT.
However, when I use polyval to plot the curve over the data points, they overlap. To find any difference between the points and the curve, I have to zoom in past the amount of significant figures in the data.
So, should I heed the warning?
댓글 수: 0
채택된 답변
Jan
2017년 3월 15일
Do not ignore the warning, because it is easy and more reliably to consider it: Call polyfit with 3 outputs to let x be scaled and shifted automatically:
[p, S, mu] = polyfit(x, y, n)
The reply mu scales the input x such that it has the mean of 0 and a standard deviation of 1. It is easy to convert the values back and e.g. polyval accepts the 4th input mu directly.
In some test cases the results might look good, but e.g. a deviation of 0.001 is not visible, but far beyond the possible accuracy.
댓글 수: 4
Steven Lord
2020년 8월 12일
Let's run an example. Here we have a pretty simple polynomial: . When we don't use scaling, the coefficients are pretty much what we expect.
>> x = 1:5;
>> y = x.^2+2*x-1;
>> Pnoscale = polyfit(x, y, 2)
Pnoscale =
1 1.99999999999999 -0.999999999999983
The coefficients when we do scale are quite different.
>> [Pscale, S, mu] = polyfit(x, y, 2);
>> Pscale
Pscale =
2.5 12.6491106406735 14
But that's because they are the coefficients of a different polynomial, not of x but of a normalized version of x. So I can keep the original x data around I'm going to use a symbolic variable z (equivalent to x) and let zz be the normalized version of x.
>> mu
mu =
3
1.58113883008419
>> syms z
>> zz = (z-mu(1))/mu(2);
So if we simplify the result of evaluating our scaled polynomial at our normalized x, do we get the same function of the unnormalized variable?
>> PscaleSymbolic = simplify(Pscale(1)*zz.^2+Pscale(2)*zz+Pscale(3))
PscaleSymbolic =
z^2 + 2*z - 1
Yes.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!