How can I interpolate data using quartic or higher order polynomials? Similar to the cubic spline interpolator csapi?
28 次查看(过去 30 天)
显示 更早的评论
How can I interpolate data using quartic or higher order polynomials? Similar to the cubic spline interpolator yy=csapi(x,y,xx) ?
4 个评论
farid anjum
2017-9-26
Thanks bro.. I got finally what i was trying to do myself since days and couldn't.
采纳的回答
John D'Errico
2015-2-24
编辑:John D'Errico
2015-2-24
You don't really want to do so. Yes, you think you might. But you don't.
High order polynomials are easy enough to build, polyfit can generate them. Just use one order less than the number of data points. So two points exactly determine a line, 5 points a quartic polynomial, etc.
That is the good news. The bad news is high order polynomials will almost always cause you to be forced to post a new anguished question, like "Why does my high order polynomial interpolant do insane things?"
In fact, depending on your data, even something relatively low order like a cubic polynomial can cause problems. Those problems typically arise from two things. The first is poorly scaled data. So if your independent variable (I'll call it x) varies over the interval [0,1000], then cubing those numbers will result in values on the order of 1e15 for a 5th order polynomial. Now, when you add and subtract numbers that vary by 15 powers of 10, expect to have numerical problems in double precision arithmetic.
High order polynomials say 15 or so, almost always tend to have numerical problems, because now you are raising your numbers to seriously high powers. The linear algebra needed to compute those coefficients often starts to have problems now, because again, it works in double precision arithmetic. So unless you are very careful about issues like scaling and centering of your data, expect problems.
Next, polynomials always SEEM like a good idea. (Hey, I can think of a lot of things I've done that SEEMED like a good idea at the time.) After all, a Taylor series is just a polynomial, and they can represent almost anything. But the fact is, polynomials are squirrelly things. They squirm around, introducing lots of added extrema to data that never did anything bad.
The classic example is seen here:
x = -10:10;
y = 1./(1+x.^2);
p = polyfit(x,y,20);
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.
> In polyfit at 75
ezplot(@(x) polyval(p,x),[-10,10])
hold on
plot(x,y,'ro')
It turns out that the squiggles you see are not due to the warning message, although scaling the data would have allowed it to work without generating a warning message.
And NEVER use a polynomial to extrapolate. Ok, linear extrapolation tends to be moderately safe. Stop there though.
Set('soapboxmode','off')
3 个评论
John D'Errico
2015-2-24
编辑:John D'Errico
2015-2-24
Admittedly, its been pretty rare that I ever really needed high order continuity in a fit. The main one I recall was when we were modeling paper paths in a copier, and third derivative discontinuities caused problems.
Regardless, why not use a higher order spline? spapi is found in the curve fitting toolbox now.
x = 0:.05:1;
y = exp(x);;
S = spapi(5,x,y);
fnplt(fnder(fnder(fnder(S))))
The 4th derivatives of that curve were not continuous.
fnplt(fnder(fnder(fnder(fnder(S)))))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!