What is the highest order polynomial I can use in the polyfit function?

38 views (last 30 days)
Hey all!
I am running a program I've made to calculate missile intercept trajectories. Currently, I have a set of data points that are given to the intercepting missile before it launches which are fit to a polynomial using polyfit. I am evaluating the polynomial to the 85th order, but I was wondering if I can evaluate it to a higher order. It is getting more and more accurate, but for some reason when I enter a larger order than 85, such as 95, it returns the polynomial's coefficients as NaN. Any reason to this? Just curious thanks!

Accepted Answer

Image Analyst
Image Analyst on 3 May 2016
If you have N data points in your 1-D array, theoretically you can use a polynomial up to order N-1. So 2 points for a line (order 1). With 3 points you can get a quadratic (order 2). And so on. Have you ever seen what happens if you fit too high an order? Yes, it will go through the training points better, but in between the training points, it goes totally crazy with HUGE oscillations. In my opinion anything more than about 4 or 5 would be unreliable at any location other than the training points.

More Answers (4)

Roland
Roland on 3 Jul 2019
As some of the colleagues pointed out, fitting a polynomial of such a high degree is normally not a good idea!
But if you really want to do this in a stable manner, you can use discrete orthogonal polynomials (DOP) for doing so! A toolbox called DOPbox is available here. Within the documentation, there is an example for fitting high degree polynomials to data.
Additionally, if you want to fit values and derivatives simultaneously (up to a high degree) you can have a look into this paper:
  • Ritt, R., Harker, M. and O’Leary, P. (2019) ‘Simultaneous Approximation of Measurement Values and Derivative Data using Discrete Orthogonal Polynomials’, arXiv Open Access Journal Article. Available at: http://arxiv.org/abs/1903.10810 (Accessed: 27 March 2019).

dpb
dpb on 3 May 2016
"... larger order than 85, such as 95, it returns the polynomial's coefficients as NaN"
Yeah, what is your range of x and what is the largest of those to the 95th power?
This is the proverbial really, really bad idea...use an interpolating spline or something similar instead.

Walter Roberson
Walter Roberson on 3 May 2016
The tests I did a while back suggested that if your data was not too spread out and you used the recentering, that you could use a degree up to roughly 6 less than the length of your data. Roughly. Sometimes you could not get as good as that.

Clayton Birchenough
Clayton Birchenough on 12 May 2016
Hey again all,
I know it's a terribly late response, but I just wanted to thank you all for the answers and helping me out.
I have only taken Calc 2, and am not yet familiar with what/how to use an interpolating spline. I'm looking into that now. The the vector I am storing my missile's x location in is ~ 3000 elements. The actual values of the elements range from 0 to 300000. I will have to further experiment with polyfit... I was aware that my polyfit function might oscillate between the test points, but when I plotted it, I failed to add points to plot between the original test points. Thanks for bringing this up again...
Like I said, just wanted to thank you all for the help. This program was my final project, and I was looking for answers quick. Got destroyed with a ton of work and soon I'm responding a month later... I am still investigating this issue, so I will try and update this when I have investigated/experimented with my program more.
Thanks again!
  3 Comments
Walter Roberson
Walter Roberson on 12 May 2016
With values ranging from 0 to 300000 then you are going to lose numeric precision for sure with anything larger than quadratic.
dpb
dpb on 13 May 2016
Standardizing the data first will help keep the range in bound of machine range but the resolution issue will still be there, indeed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!