1

I have 1x1024 matrix. So I'd like to estimate a polynomial equation.

X= (0:1023)'
Y= acquired data.  A 1024 element vector

Then I try this in MATLAB:

polyfit(x,y,5)

But MATLAB makes an abnormal result with warning.

Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the ...

I don't understand what am I doing wrong?

Update

I got a bunch of numbers like this.

Y=

-0.0000000150
 ...
0.00001
...
0
...
0.17

X= 0~255

polyfit(X,Y,4)

I got a polynomial but it does not match to original curve. Is there any options to match between original curve and polyfit's curve?

Community
  • 1
  • 1
bural
  • 45
  • 1
  • 7

2 Answers2

3

The problem can be attributed to the type of coefficient matrix that polyfit builds from the x vector: a Vandermonde matrix.

When

  • the elements of the x vector vary too much in magnitude, and
  • the degree of the fitting polynomial is too high,

you get an ill-conditioned matrix, and the associated linear system cannot be solved reliably.

Try to centre and scale your x vector first, before applying polyfit, as advised at the bottom of the polyfit help page:

Since the columns in the Vandermonde matrix are powers of the vector x, the condition number of V is often large for high-order fits, resulting in a singular coefficient matrix. In those cases centering and scaling can improve the numerical properties of the system to produce a more reliable fit.

(my emphasis)

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 1
    I didn't see this in the documentation. Very nice. +1. – rayryeng Aug 24 '15 at 16:33
  • @rayryeng It's easy to miss, but I remember running into a similar problem once. I had already upvoted your answer :) I didn't mean to steal the checkmark :p – jub0bs Aug 25 '15 at 05:18
  • Eh it happens lol. Not the first time someone came around with a better answer and the checkmark goes lol. It makes sense to normalize and scale the points first. Wish I had thought of that even in an intuitive sense. – rayryeng Aug 25 '15 at 05:49
2

The warning is because the data that you are supplying to polyfit with your desired degree of polynomial isn't suitable. Specifically, there is an insufficient amount of variability in your data so that you can successfully achieve a good fit. Therefore, MATLAB gives you that warning because the data can't be fit properly with your desired degree polynomial.

The solution to this is to either get more points so that you can get the desired fit of the polynomial degree you want or to decrease the degree of polynomial you want.

Try values that are less than 5... 4, 3 or perhaps 2:

coeff = polyfit(x, y, 4);
%// or
%coeff = polyfit(x, y, 3);
%coeff = polyfit(x, y, 2);

Try each degree until you don't get the warning anymore. However, without the actual data, I can only speculate what's wrong, and this is my best guess.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Thanks. Are there any options in my case instead of adding point or using polyfit? – bural Aug 24 '15 at 01:38
  • Can't say unless I see your data. – rayryeng Aug 24 '15 at 01:41
  • I found pinv() function at Matlab, is this make result better than polyfit()? – bural Aug 24 '15 at 07:09
  • `polyfit` basically finds the polynomial coefficients by Least Squares: https://en.wikipedia.org/wiki/Polynomial_regression#Matrix_form_and_calculation_of_estimates. `pinv` is essentially used in `polyfit`. If you want to continue using polynomial regression, you should try using something from the Curve Fit Toolbox instead that is more robust: http://www.mathworks.com/help/curvefit/linear-and-nonlinear-regression.html – rayryeng Aug 24 '15 at 07:30