MATLAB Answers

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

조회 수: 68(최근 30일)
Clayton Birchenough
Clayton Birchenough 2016년 5월 3일
답변: Roland 2019년 7월 3일
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!

채택된 답변

Image Analyst
Image Analyst 2016년 5월 3일
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.

추가 답변(4개)

Roland 2019년 7월 3일
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: (Accessed: 27 March 2019).

dpb 2016년 5월 3일
"... 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 2016년 5월 3일
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 2016년 5월 12일
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
dpb 2016년 5월 13일
Standardizing the data first will help keep the range in bound of machine range but the resolution issue will still be there, indeed.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by