How can I interpolate data using quartic or higher order polynomials? Similar to the cubic spline interpolator csapi?

39 views (last 30 days)
How can I interpolate data using quartic or higher order polynomials? Similar to the cubic spline interpolator yy=csapi(x,y,xx) ?
  4 Comments

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 24 Feb 2015
Edited: John D'Errico on 24 Feb 2015
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 Comments
John D'Errico
John D'Errico on 24 Feb 2015
Edited: John D'Errico on 24 Feb 2015
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)))))
Clay Fulcher
Clay Fulcher on 24 Feb 2015
Edited: Clay Fulcher on 24 Feb 2015
Awesome. This pried me loose, and is what I was looking for. I did the following also, so that I could interpolate to a specific set of xx points:
x = 0:.05:1;
y = exp(x);
S = spapi(5,x,y);
xx= 0:.0001:1;
S = fnval(xx,S)';
plot(diff(S,3));
figure
plot(diff(S,4));
The 3rd derivative is smooth and the 4th derivative is very small, approx 0.
Thanks man!

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!