Obtaining smoother polynomials from polyfit for curve fitting (Best Fit Curve) or an alternate function.

117 views (last 30 days)
I am using polyfit to analyze data and perform a curve fit wither several different order polynomials. The end goal is to develop a best fit line for the data that is very smooth (no waves between known data points). Basically, what I want is a smooth best fit line simmilar to using excel. In excell I can ask for up to a 6th order polynomial. It will try to fit my data in such away that the curve is very smooth. When I use polyfit the result tends to be quite wavy in between the known data points. I think the problem lies in the fact that polyfit tries to exactly match the know data points I supply it. Whereas excell will yield polynomials that are not exact matches to the data, but do yield a nice, smooth fit. The end goal is to use these curves to predict the models behavior. This means that having wiggles/waves in the curves is a big problem.
I have not found anything in polyfit's documentation that says it has any options. Could anyone suggest a way I could obtain results more simmilar to excel, be that changes with to how I use polyfit or an alternative function that I am unaware of?
I can't right now, but if necessary I can supply sample code later.

Accepted Answer

bym
bym on 11 Jun 2012
What you are describing sounds like Runge's phenomenon: Check out this blog entry

More Answers (3)

Tom Lane
Tom Lane on 12 Jun 2012
It's hard to say what's going on without seeing your data or results. If you have more than 7 points with distinct x values, the polyfit function will in fact perform least squares and not force the fit to pass through the points.
One polyfit option that's easy to overlook is the following. If you request a third output from the polyfit function, the function will recenter and rescale your data. Sometimes that improves the results. You just need to undo that adjustment when you use the coefficients for prediction. The polyval function can handle that for you.

Alfonso
Alfonso on 19 Jun 2012
Thanks for the responses, sorry I wasn't more detailed. Tom, your answer was also a big help. Currently I'm stuck with a small number of points for each run (5-6). I added a few atrifical points in between for a test and got significantly different results more like what i was hoping for. Not perfect of course, polynomials act like polynomials after all!

John D'Errico
John D'Errico on 26 Jan 2017
It is WAY too late for this answer to be of any help. A high order polynomial fit is almost always bad, if for example, you use a degree n-1 polynomial for n points, will almost always produce total crap.
Polynomial fits are not bad as long as you stay low in order. quadratic or cubic is about as high as I usually advise. If your curve is too complex for a medium order polynomial (say at most 5th degree) then I strongly advise using a spline model.
An interpolating spline can often exhibit ringing. The worst possible curve is a step function of course. For such problems, I usually advise a tool like pchip (one of the options in interp1, as well as the function by that name.)
But not all curves are correct to use interpolation. If you have noise, then you will need to use a smoothing tool. A smoothing spline is a good choice some of the time. You can use my SLM toolbox on a wide variety of problems, as it gives you a huge variety of bells and whistles, ranging from an interpolant to a smoothing/least squares spline. You also have control over the shape of the resulting function, something that other tools do not offer. In fact, most of the time, this is the tool I advise for general use, since it can avoid the nasty oscillations even on step function data, and it can handle noise.
  1 Comment
B
B on 22 Nov 2017
So you're saying that I can find the equation of a sixth degree polynomial without using polyfit. Could you please explain how I could use the spline model?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!