Warning says polynomial is badly conditioned, but it seems to fit with the data

147 ビュー (過去 30 日間)
Jacqueline
Jacqueline 2017 年 3 月 15 日
コメント済み: Jannik M. 2020 年 11 月 4 日
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?

採用された回答

Jan
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
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.
Jannik M.
Jannik M. 2020 年 11 月 4 日
See also my answer here on how to denormalized the coefficients without using the Symbolic Math Toolbox.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by