MATLAB Answers

Why is the POLYFIT function in MATLAB unable to find a fit over my data values?

73 views (last 30 days)
I am using the POLYFIT function to fit a second order polynomial over my data values as follows.
x = -100000:10000:200000;
f = @(x) 1000*x.^2+ 3*x + 0.023;
y = f(x);
polyfit(x,y,2)
However, I receive the following warning message 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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
The reason why you get this error message is because the matrix of predictors as defined by the data in X is ill-conditioned. For example, if one had 20 sets of observations (X,Y) and wanted to fit a 2nd order polynomial, the predictor matrix would be 20 x 3 (20 rows for each of the observations and 3 columns for each of the coefficients (C,B,A) of 1, X,X^2. The fitting process requires this matrix to be well conditioned.
One of the ways to ensure this is to scale the data, such that each X is transformed as follows:
X_new = (X mean(X))/std(X)
This ensures that we are working with a small range of X_new values, [-1 1].
The third calling syntax of POLYFIT essentially does this scaling before fitting the polynomial. As a result, the coefficients returned are different but correct in the sense that if one inputs a scaled X value you get the right Y value.
If you would like the coefficients for the un-scaled input X (C,B,A) , you need to transform (C_new,B_new,A_new) as shown in the code below. Note that these hold only for a 2nd order polynomial.
 
[p,S,mu] = polyfit(X,y,2)
A_new = p(1); B_new = p(2); C_new = p(3); mu1 = mu(1); mu2 = mu(2);
C=A_new*(mu1)^2/(mu2)^2-(B_new*(mu1/mu2))+C_new 
B = (B_new*mu2 2*A_new*mu1)/(mu2)^2
A = A_new/(mu2)^2
mu1 and mu2 are the first and second elements of the third output of POLYFIT.
The above relationships are obtained for by solving the following equation
A_new*(X_new)^2 +B_new *(X_new) + C_new = A*(X)^2 + B*(X) + C
Where X_new is given by the relationship above.
One can similarly, find relationships for higher order polynomials.
  3 Comments
Shaik Pasha
Shaik Pasha on 27 Oct 2014
Hi, the provided information for 2nd order polynomial is very much useful. But my query is what would be the transformation equations for the 3rd and 4th order polynomial equations, wherein i had to extract [A,B,C,D] and [A,B,C,D,E], respectively.
Hope for the earliest reply.

Sign in to comment.

More Answers (2)

Shaik Pasha
Shaik Pasha on 27 Oct 2014
Hi, the provided information for 2nd order polynomial is very much useful. But my query is what would be the transformation equations for the 3rd and 4th order polynomial equations, wherein i had to extract [A,B,C,D] and [A,B,C,D,E], respectively.
Hope for the earliest reply.
  1 Comment
Jan
Jan on 27 Oct 2014
But as far as I can see it is trivial to expand the formula for further parameters. And most of all, this is not needed when you apply polyval to evaluate the polynomial and provide S and mu as input. Therefore you can look in the code of polyval how to solve this for an arbitrary degree.
Please do not post a question twice.

Sign in to comment.


Christoph
Christoph on 31 Oct 2014
Edited: MathWorks Support Team on 6 Apr 2018
@Shaik: Basically, you have to exploit the binomial theorem http://en.wikipedia.org/wiki/Binomial_theorem to get a formula for arbitrary degrees of your polynomial. A working MATLAB solution can be found here: https://groups.google.com/forum/#!topic/comp.soft-sys.matlab/D4JPQhkylkE - the polyfit_convert function.

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!