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

82 views (last 30 days)

Show older comments

Clay Fulcher
on 24 Feb 2015

Commented: Utkarsh Deshmukh
on 14 Dec 2017

##### 4 Comments

### Accepted Answer

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

### More Answers (0)

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!