9.2: Higher Order Polynomial Fitting
- Page ID
- 14965
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
Let’s try fitting a fifth degree polynomial to the data in example 38.
Example 9.2.1
>> x=0:5;y=[12,10,9,6,2,0];
>> coeffs5=polyfit(x,y,5)
returns
coeffs5 =
-0.0167 0.3333 -2.0833 4.6667 -4.9000 12.0000
which are the coefficients for the approximating 5th order polynomial, namely
y = −0.0167x5 + 0.3333x4 − 2.0833x3 + 4.6667x2 − 4.9x + 12.
We could type out the full polynomial, but there is a shortcut. We can use the function polyval along with linspace to give a smooth approximating curve.
>> x5=linspace(0,5);
>> y5=polyval(coeffs5,x5);
We plot the curves
>> plot(x,y,’o’,x,besty,x5,y5)