How can I interpolate data using quartic or higher order polynomials? Similar to the cubic spline interpolator csapi?
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Clay Fulcher
el 24 de Feb. de 2015
Comentada: Utkarsh Deshmukh
el 14 de Dic. de 2017
How can I interpolate data using quartic or higher order polynomials? Similar to the cubic spline interpolator yy=csapi(x,y,xx) ?
4 comentarios
farid anjum
el 26 de Sept. de 2017
Thanks bro.. I got finally what i was trying to do myself since days and couldn't.
Respuesta aceptada
John D'Errico
el 24 de Feb. de 2015
Editada: John D'Errico
el 24 de Feb. de 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 comentarios
John D'Errico
el 24 de Feb. de 2015
Editada: John D'Errico
el 24 de Feb. de 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)))))
Más respuestas (0)
Ver también
Categorías
Más información sobre Polynomials en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!