Curve Fitting using polyfit and polyval

Tutorial Video

Generating Perfect Model Data

Let's generate some model x1 and y1 data. First we will create an independent variable x1 that is a column vector ranging from 0 to 20 in steps of 2 and then we can create y1 which is a polynomial with respect to x1. We will store this in a table called data:

x1=<span style="color: #ff0000;">[</span>0<span style="color: #ff0000;">:</span>2<span style="color: #ff0000;">:</span>20<span style="color: #ff0000;">]</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;</span>
y1=<span style="color: #000000;">0.1*x1<span style="color: #ff0000;">.</span>^2+1.2*x1+5</span><span style="color: #ff00ff;">;</span>
data=<span style="color: #0000ff;">table(</span>x1<span style="color: #808080;">,</span>y1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">x1</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">y1</span>;</span>

For this quadratic polynomial

y1=<span style="color: #000000;"><span style="color: #ff0000;">0.1</span>*x1.^2+<span style="color: #ff0000;">1.2</span>*x1+<span style="color: #ff0000;">5</span></span>

of the form

y1=<span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #0000ff;">p2(</span><span style="color: #000000;">1</span><span style="color: #0000ff;">)</span></span>*x1.^2+<span style="color: #ff0000;"><span style="color: #0000ff;">p2(</span><span style="color: #000000;">2</span><span style="color: #0000ff;">)</span></span>*x1+<span style="color: #ff0000;"><span style="color: #0000ff;">p2(</span><span style="color: #000000;">3</span><span style="color: #0000ff;">)</span></span></span>

the three coefficients are:

<span style="color: #3366ff;">p2(</span>1<span style="color: #3366ff;">)</span>=<span style="color: #ff0000;">0.1</span>
<span style="color: #3366ff;">p2(</span>2<span style="color: #3366ff;">)</span>=<span style="color: #ff0000;">1.2</span>
<span style="color: #3366ff;">p2(</span>3<span style="color: #3366ff;">)</span>=<span style="color: #ff0000;">5</span>
<span style="color: #0000ff;">p2(</span>1<span style="color: #0000ff;">)</span>

is the 

x.^2

 coefficient 

<span style="color: #0000ff;">p2(</span>2<span style="color: #0000ff;">)</span>

is the 

x

coefficient and

<span style="color: #0000ff;">p2(</span>3<span style="color: #0000ff;">)</span>

 is the constant.

Scatter Plot

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">scatter(<span style="color: #000000;">data<span style="color: #ff0000;">.</span>x1</span></span><span style="color: #808080;">,<span style="color: #000000;">data<span style="color: #ff0000;">.</span>y1</span></span><span style="color: #808080;">,</span>100<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">fill</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeColor</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>[<span style="color: #ff0000;">112/255</span><span style="color: #808080;">,</span><span style="color: #339966;">48/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">160/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceColor</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span>[<span style="color: #ff0000;">0/255</span><span style="color: #808080;">,</span><span style="color: #339966;">176/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">80/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>1<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #0000ff;">)</span>
<span style="color: #0000ff;">set(</span>gca,<span style="color: #0000ff;"><span style="color: #800080;">'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">x</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Polynomial</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)</span>
<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y1</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthEast</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>


Using polyfit to Find the Polynomial Coefficients

We can use polyfit to evaluate the polynomial coefficients. Polyfit has the form

<span style="color: #ff0000;">[</span>p<span style="color: #ff00ff;">n</span><span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span>x,y,<span style="color: #ff00ff;">n</span><span style="color: #3366ff;">)</span>

Where the input arguments are the x data, y data and n, the order of polynomial. The output arguments is a row vector pn of the polynomial coefficients. In most cases we would try fitting from a first order polynomial upwards however as we know our data is a second order polynomial, as it is created from a second order polynomial we can try fitting to a second order polynomial first.

<span style="color: #ff0000;">[</span>p<span style="color: #ff00ff;">2</span><span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span><span style="color: #666699;"><span style="color: #000000;">data<span style="color: #ff0000;">.</span>x1,data<span style="color: #ff0000;">.</span>y1</span></span>,<span style="color: #ff00ff;">2</span><span style="color: #3366ff;">)</span>

\displaystyle \text{p2=}\left[ {\begin{array}{*{20}{c}} {0.100000} & {1.200000} & {5.000000} \end{array}} \right]

These are the three polynomial coefficients for the quadratic equation:

\displaystyle 0.100000{{x}^{2}}+1.200000x+5.000000

The original polynomial was

y1=<span style="color: #000000;"><span style="color: #ff0000;">0.1</span>*x1.^2+<span style="color: #ff0000;">1.2</span>*x1+<span style="color: #ff0000;">5</span></span>

and the three coefficients returned match

y1=<span style="color: #000000;"><span style="color: #ff0000;"><span style="color: #0000ff;">p2(</span>1<span style="color: #0000ff;">)</span></span>*x1.^2+<span style="color: #ff0000;"><span style="color: #0000ff;">p2(</span>2<span style="color: #0000ff;">)</span></span>*x1+<span style="color: #ff0000;"><span style="color: #0000ff;">p2(</span>3<span style="color: #0000ff;">)</span></span></span>

as expected.

Which as expected is a perfect match to our original equation.


Using polyval to Evaluate the Polynomial

We can use polyval to evaluate the polynomial at specified x values. Let's first create a new variable x2 and assign it as a column in a new table called datafit.

<span style="color: #000000;"><span style="color: #3366ff;">x2</span></span>=<span style="color: #ff0000;">[</span>1:2:19<span style="color: #ff0000;">]'<span style="color: #ff00ff;">;
<span style="color: #000000;">datafit=</span><span style="color: #0000ff;">table(</span><span style="color: #000000;">x2</span><span style="color: #0000ff;">)</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">x2</span>;</span></span>
 

We can use polyval to evaluate the polynomial coefficients. polyval has the form

<span style="color: #ff0000;">[<span style="color: #000000;">new</span></span>y<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyval(</span>p<span style="color: #ff00ff;">n</span><span style="color: #808080;">,</span>newx<span style="color: #3366ff;">)<span style="color: #ff00ff;">;</span></span>

We can use the function

<span style="color: #ff0000;">[</span>datafit<span style="color: #ff0000;">.</span>y2<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyval(</span><span style="color: #000000;">p2</span><span style="color: #808080;">,</span>datafit<span style="color: #ff0000;">.</span>x2<span style="color: #3366ff;">)<span style="color: #ff00ff;">;</span></span>

To find the value of the polynomial for each

x2

value.


Plotting the Fitted Data with Respect to the Raw Data

We can plot this data as a scatter plot alongside the original data:

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">scatter(<span style="color: #000000;">data<span style="color: #ff0000;">.</span>x1</span></span><span style="color: #808080;">,<span style="color: #000000;">data<span style="color: #ff0000;">.</span>y1</span></span><span style="color: #808080;">,</span>100<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">fill</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeColor</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>[<span style="color: #ff0000;">112/255</span><span style="color: #808080;">,</span><span style="color: #339966;">48/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">160/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceColor</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span>[<span style="color: #ff0000;">0/255</span><span style="color: #808080;">,</span><span style="color: #339966;">176/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">80/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>1<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #0000ff;">)</span>
<span style="color: #0000ff;">hold(<span style="color: #800080;">'</span><span style="color: #ff9900;">on</span><span style="color: #800080;">'<span style="color: #0000ff;">)<span style="color: #ff00ff;">;</span>
scatter(<span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>x2</span><span style="color: #808080;">,<span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>y2</span></span><span style="color: #808080;">,</span><span style="color: #000000;">100</span><span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">fill</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeColor</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #000000;">[</span><span style="color: #ff0000;">255/255</span><span style="color: #808080;">,</span><span style="color: #339966;">0/255</span><span style="color: #808080;">,</span>0/255<span style="color: #000000;">]</span><span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceColor</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #000000;">[</span><span style="color: #ff0000;">0/255</span><span style="color: #808080;">,</span><span style="color: #339966;">112/255</span><span style="color: #808080;">,</span>92/255<span style="color: #000000;">]</span><span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #000000;">1</span><span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5)<span style="color: #ff00ff;">;</span></span></span>
hold(<span style="color: #800080;">'</span><span style="color: #ff9900;">off</span><span style="color: #800080;">'</span>)<span style="color: #ff00ff;">;</span>
set(</span>gca,<span style="color: #0000ff;"><span style="color: #800080;">'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">x</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Polynomial</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)</span>
<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y1</span><span style="color: #800080;">'</span><span style="color: #808080;">,<span style="color: #800080;">'</span><span style="color: #ff9900;">y2</span><span style="color: #800080;">',</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthEast</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

It may be more common to plot the fitted curve as a line:

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">scatter(<span style="color: #000000;">data<span style="color: #ff0000;">.</span>x1</span></span><span style="color: #808080;">,<span style="color: #000000;">data<span style="color: #ff0000;">.</span>y1</span></span><span style="color: #808080;">,</span>100<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">fill</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeColor</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>[<span style="color: #ff0000;">112/255</span><span style="color: #808080;">,</span><span style="color: #339966;">48/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">160/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceColor</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span>[<span style="color: #ff0000;">0/255</span><span style="color: #808080;">,</span><span style="color: #339966;">176/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">80/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>1<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #0000ff;">)</span>
<span style="color: #0000ff;"><span style="color: #0000ff;">hold(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">on</span><span style="color: #800080;"><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #ff00ff;"><span style="color: #800080;"><span style="color: #0000ff;">plot(</span><span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>x2</span><span style="color: #808080;">,<span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>y2<span style="color: #808080;">,<span style="color: #ff00ff;">...</span>
</span><span style="color: #800080;">'</span><span style="color: #ff9900;">color</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">r</span><span style="color: #800080;">'<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
'<span style="color: #ff9900;">LineWidth</span>'<span style="color: #808080;">,</span><span style="color: #000000;">3</span></span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span></span></span></span></span></span>
<span style="color: #0000ff;">hold(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">off</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">set(</span></span>gca,<span style="color: #0000ff;"><span style="color: #800080;">'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">x</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Polynomial</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)</span>
<span style="color: #3366ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y1</span><span style="color: #800080;">'</span><span style="color: #808080;">,<span style="color: #800080;">'</span><span style="color: #ff9900;">y2</span><span style="color: #800080;">',</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">Location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">NorthEast</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>

If we want to print the equation to the chart we can use:

equation=<span style="color: #0000ff;">sprintf(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">y=(</span><span style="color: #99cc00;">%f</span><span style="color: #ffcc00;">)x^2+(</span><span style="color: #ff0000;">%f</span><span style="color: #ffcc00;">)x+(</span><span style="color: #800000;">%f</span><span style="color: #ffcc00;">)</span><span style="color: #800080;">'</span>,<span style="color: #99cc00;">p2(1)</span>,<span style="color: #ff0000;">p2(2)</span>,<span style="color: #800000;">p2(3)</span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">legend(</span>'<span style="color: #ffcc00;">data</span>',equation,<span style="color: #800080;">'</span><span style="color: #ffcc00;">location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">northwest</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">FontSize</span><span style="color: #800080;">'</span>,8<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>

The function

<span style="color: #0000ff;">sprintf</span>

has the same form as

<span style="color: #0000ff;">fprintf</span>

but prints to a string variable opposed to outputting in the Command Window.


Underfitting – Fitting a 1st Order Polynomial to 2nd Order Polynomial Data

The code above can be modified to fit to a 1st order polynomial (i.e. a straight line). Here it is obvious by eye that it is an unsuitable fit:

The reader is recommended to try to use polyfit and polyval to try and fit the above curve to a 1st order polynomial (straight line).

<span style="color: #000000;"><span style="color: #3366ff;">x2</span></span>=<span style="color: #ff0000;">[</span>1:2:19<span style="color: #ff0000;">]'<span style="color: #ff00ff;">;
<span style="color: #000000;"><span style="color: #ff0000;">[</span>p1<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span><span style="color: #666699;"><span style="color: #000000;">data.x1</span><span style="color: #808080;">,</span><span style="color: #000000;">data.y1</span></span><span style="color: #808080;">,</span>1<span style="color: #3366ff;">)</span></span>
<span style="color: #000000;">datafit=</span><span style="color: #0000ff;">table(</span><span style="color: #000000;">x2</span><span style="color: #0000ff;">)</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">x2</span>;<span style="color: #000000;">
<span style="color: #ff0000;">[</span>datafit.y2<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyval(</span>p1<span style="color: #808080;">,</span>datafit.x2<span style="color: #3366ff;">);</span>
</span></span></span>

The user is recommended to plot the graph above, with the updated equation in the legend.

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">scatter(<span style="color: #000000;">data<span style="color: #ff0000;">.</span>x1</span></span><span style="color: #808080;">,<span style="color: #000000;">data<span style="color: #ff0000;">.</span>y1</span></span><span style="color: #808080;">,</span>100<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">fill</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeColor</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>[<span style="color: #ff0000;">112/255</span><span style="color: #808080;">,</span><span style="color: #339966;">48/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">160/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceColor</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span>[<span style="color: #ff0000;">0/255</span><span style="color: #808080;">,</span><span style="color: #339966;">176/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">80/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>1<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #0000ff;">)</span>
<span style="color: #0000ff;">hold(<span style="color: #800080;">'</span><span style="color: #ff9900;">on</span><span style="color: #800080;">')<span style="color: #ff00ff;">;</span>
<span style="color: #ff00ff;">plot(<span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>x2</span><span style="color: #808080;">,<span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>y2,...
'<span style="color: #ff9900;">color</span>','<span style="color: #ff9900;">r</span>',...
'<span style="color: #ff9900;">LineWidth</span>',3);</span></span></span></span>
hold(<span style="color: #800080;">'</span><span style="color: #ff9900;">off</span><span style="color: #800080;">'</span>)<span style="color: #ff00ff;">;</span>
set(</span>gca,<span style="color: #0000ff;"><span style="color: #800080;">'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;">,20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">x</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Polynomial</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)
<span style="color: #000000;">equation=</span><span style="color: #0000ff;">sprintf(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">y=(</span><span style="color: #99cc00;">%f</span><span style="color: #ffcc00;">)x+(</span><span style="color: #ff0000;">%f</span><span style="color: #ffcc00;">)</span><span style="color: #800080;">'</span>,<span style="color: #99cc00;">p2(1)</span>,<span style="color: #ff0000;">p2(2)</span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">legend(</span>'<span style="color: #ffcc00;">data</span>',equation,<span style="color: #800080;">'</span><span style="color: #ffcc00;">location</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">northwest</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">FontSize</span><span style="color: #800080;">'</span>,8<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span></span>


Overfitting – Fitting a 3rd Order Polynomial to 2nd Order Polynomial Data

Here from the values of the coefficients i.e. the cubic term being extremely small 1.6509e-17 it is obvious that the polynomial is overfitted. This term can be rounded to 0 and we return with the quadratic fit. This is perfect model data so the fitting is relatively easy in real life it may be harder to distinguish data which has been over-fitted.

The reader is recommended to try to use polyfit and polyval to try and fit the above curve to a 3rd order polynomial.

<span style="color: #000000;"><span style="color: #3366ff;">x2</span></span>=<span style="color: #ff0000;">[</span>1:2:19<span style="color: #ff0000;">]'<span style="color: #ff00ff;">;
<span style="color: #000000;"><span style="color: #ff0000;">[</span>p3<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span><span style="color: #666699;"><span style="color: #000000;">data.x1</span><span style="color: #808080;">,</span><span style="color: #000000;">data.y1</span></span><span style="color: #808080;">,</span>3<span style="color: #3366ff;">)</span></span>
<span style="color: #000000;">datafit=</span><span style="color: #0000ff;">table(</span><span style="color: #000000;">x2</span><span style="color: #0000ff;">)</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">x2</span>;<span style="color: #000000;">
<span style="color: #ff0000;">[</span>datafit.y2<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyval(</span>p3<span style="color: #808080;">,</span>datafit.x2<span style="color: #3366ff;">);</span>
</span></span></span>

The user is recommended to plot the graph above, with the updated equation in the legend.

<span style="color: #0000ff;">figure(</span>1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">scatter(<span style="color: #000000;">data<span style="color: #ff0000;">.</span>x1</span></span><span style="color: #808080;">,<span style="color: #000000;">data<span style="color: #ff0000;">.</span>y1</span></span><span style="color: #808080;">,</span>100<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ff9900;">fill</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeColor</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>[<span style="color: #ff0000;">112/255</span><span style="color: #808080;">,</span><span style="color: #339966;">48/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">160/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceColor</span><span style="color: #800080;">'<span style="color: #808080;">,</span></span>[<span style="color: #ff0000;">0/255</span><span style="color: #808080;">,</span><span style="color: #339966;">176/255</span><span style="color: #808080;">,</span><span style="color: #0000ff;">80/255</span>]<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>1<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFaceAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #808080;">,</span><span style="color: #ff00ff;">...</span>
<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdgeAlpha</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>0.5<span style="color: #0000ff;">)</span>
<span style="color: #0000ff;">hold(<span style="color: #800080;">'</span><span style="color: #ff9900;">on</span><span style="color: #800080;">')<span style="color: #ff00ff;">;</span>
<span style="color: #ff00ff;">plot(<span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>x2</span><span style="color: #808080;">,<span style="color: #000000;">datafit<span style="color: #ff0000;">.</span>y2,...
'<span style="color: #ff9900;">color</span>','<span style="color: #ff9900;">r</span>',...
'<span style="color: #ff9900;">LineWidth</span>'<span style="color: #808080;">,</span>3);</span></span></span></span>
hold(<span style="color: #800080;">'</span><span style="color: #ff9900;">off</span><span style="color: #800080;">'</span>)<span style="color: #ff00ff;">;</span>
set(</span>gca,<span style="color: #0000ff;"><span style="color: #800080;">'<span style="color: #ff9900;">FontSize</span>'<span style="color: #000000;"><span style="color: #808080;">,</span>20</span></span>)</span>
<span style="color: #3366ff;">xlabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">x</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">ylabel(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">y</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">title(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Polynomial</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">grid(<span style="color: #ffcc00;"><span style="color: #800080;">'</span><span style="color: #ff9900;">minor</span><span style="color: #800080;">'</span></span>)
<span style="color: #000000;">equation=</span><span style="color: #0000ff;">sprintf(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">y=(</span><span style="color: #99cc00;">%f</span><span style="color: #ffcc00;">)x^3+(</span><span style="color: #ff0000;">%f</span><span style="color: #ffcc00;">)x^2+(<span style="color: #008000;">%f</span>)x+(<span style="color: #800000;">%f</span>)</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #99cc00;">p3(1)</span><span style="color: #808080;">,</span><span style="color: #ff0000;">p3(2)<span style="color: #808080;">,</span><span style="color: #008000;">p3(3)</span><span style="color: #808080;">,</span><span style="color: #800000;">p3(4)</span></span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
<span style="color: #0000ff;">legend(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">data</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span>equation<span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">location</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">northwest</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">FontSize</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #000000;">8</span><span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span></span>


Generating Model Data with Noise

<span style="color: #3366ff;">rng(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">default</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span><span style="color: #ff00ff;">;</span>
x1=<span style="color: #ff0000;">[</span>0<span style="color: #ff0000;">:</span>2<span style="color: #ff0000;">:</span>20<span style="color: #ff0000;">]</span><span style="color: #800080;">'</span><span style="color: #ff00ff;">;
</span><span style="color: #ff0000;">[</span>m<span style="color: #808080;">,</span>n<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">size(</span>x1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;
</span>noise=<span style="color: #0000ff;">rand(</span>m<span style="color: #808080;">,</span>n<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;</span>
y1=<span style="color: #000000;">0.1*x1<span style="color: #ff0000;">.</span>^2+1.2*x1+5+noise</span><span style="color: #ff00ff;">;</span>
data=<span style="color: #0000ff;">table(</span>x1<span style="color: #808080;">,</span>y1<span style="color: #0000ff;">)</span><span style="color: #ff00ff;">;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">x1</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">y1</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">noise</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">m</span>;
<span style="color: #0000ff;">clear</span> <span style="color: #000000;">n</span>;</span>

We see a deviation from the starting polynomial with:

<span style="color: #3366ff;">p2(</span>1<span style="color: #3366ff;">)</span>=<span style="color: #ff0000;">0.1</span>
<span style="color: #3366ff;">p2(</span>2<span style="color: #3366ff;">)</span>=<span style="color: #ff0000;">1.2</span>
<span style="color: #3366ff;">p2(</span>3<span style="color: #3366ff;">)</span>=<span style="color: #ff0000;">5</span>

The deviation influences the constant term the most, followed by the x term.

If we times the noise by 5, we see a larger deviation:

If we times the noise by 10 we see more deviation:

And if we times the noise by 25 we see an even larger deviation again:

If we times the noise by 250, the original function is not recognisable:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.