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

113 views (last 30 days)
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?

Accepted Answer

Jan
Jan on 15 Mar 2017
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 Comments
Steven Lord
Steven Lord on 12 Aug 2020
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. on 4 Nov 2020
See also my answer here on how to denormalized the coefficients without using the Symbolic Math Toolbox.

Sign in to comment.

More Answers (0)

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!