Obtaining smoother polynomials from polyfit for curve fitting (Best Fit Curve) or an alternate function.
47 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
更多回答(3 个)
Tom Lane
2012-6-12
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.
John D'Errico
2017-1-26
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 个评论
B
2017-11-22
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!