Warning says polynomial is badly conditioned, but it seems to fit with the data
84 次查看(过去 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/345123/image.png)
>> 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!