Fitting a Curve Using Polyfit and Polyval

Generating Model Data

Let's generate some model x and y data. First we will create an independent variable x that is a column vector ranging from 0 to 20 in steps of 2.

x=<span style="color: #ff0000;">[</span>0:2:20<span style="color: #ff0000;">]'</span>

We will then create the dependent variable y which will be a model quadratic function of x.

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

Note that we use

.^

to perform element by element exponentiation opposed to

^

which would perform array exponentiation. It then follows that y is a 2nd order polynomial with coefficients

<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>

For convenience we can save these as the first and second column in a sheet rawdata

<span style="color: #666699;">rawdata(</span>:,1<span style="color: #666699;">)</span>=x
<span style="color: #666699;">rawdata(</span>:,2<span style="color: #666699;">)</span>=y


Plotting the Raw Data

To suppress the output in the command window, we can end each of the lines above in a semicolon

;

We can instead go ahead and plot the second column of rawdata (y) with respect to the first column of rawdata (x) as figure(2). We want blue filled markers of size 8, no line. For the axes we want a fontsize of 20, gridlines and x and y labels. If unsure of any of this code see Plotting 2D Figures in Octave/MATLAB.

<span style="color: #3366ff;">figure(</span>2<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span><span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<span style="color: #800080;">'<span style="color: #ff9900;">o</span>'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>,8,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">b</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">b</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">on</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">minor</span>;
<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</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>


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;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<span style="color: #ff00ff;">2</span><span style="color: #3366ff;">)</span>

<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;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</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

 
P2(1)

is the 

x.^2

 coefficient 

P2(2)

is the 

x

 coefficient and

P2(3)

 is the constant.

Which as expected is a perfect match to our original equation

y=<span style="color: #ff0000;">0.1</span>*x.^2+<span style="color: #ff0000;">1.2</span>*x+<span style="color: #ff0000;">5</span>
<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>

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 called fitteddata and to it's first column we can specify new x values. For illustration purposes, to make the new x values slightly different to the original x data we can use all the odd numbers opposed to the even numbers. Thus

<span style="color: #3366ff;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #3366ff;">)</span>=<span style="color: #ff0000;">[</span>1:2:19<span style="color: #ff0000;">]'</span>

If you prefer you can use the same original x data to evaluate the polynomial.

Now that we have the x data to evaluate 

<span style="color: #3366ff;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #3366ff;">)</span>

 the polynomial with coefficients in

P2

we can use Polyval. Polyval has the form

<span style="color: #ff0000;">[</span>evaluatedydata<span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyval(</span>P<span style="color: #ff00ff;">n</span>,evaluatedxdata<span style="color: #3366ff;">)</span>

In this example I will created a new column in fitteddata, column 3 (as I will later use column 2 to look at a linear fit)

<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">3</span><span style="color: #666699;">)</span>=<span style="color: #3366ff;">polyval(</span>P<span style="color: #ff00ff;">2</span>,<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span><span style="color: #3366ff;">)</span>


Plotting the Fitted Data with Respect to the Raw Data

We can once again suppress the output into the command window and instead look at graphical output. For convenience, the graphing will be moved to the bottom, at the bottom of the graph we can add the hold on and hold off commands and plot the fitted data in the same figure as the original data

<span style="color: #3366ff;">figure(</span>2<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span><span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<span style="color: #800080;">'<span style="color: #ff9900;">o</span>'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>,8,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">b</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">b</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">on</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">minor</span>;
<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</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;">hold</span> <span style="color: #ff0000;">on</span>;
<span style="color: #3366ff;">plot(</span><span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">3</span><span style="color: #666699;">)</span>,<span style="color: #800080;">'<span style="color: #ff9900;">o</span>'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>,8,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">g</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">g</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>;
<span style="color: #3366ff;">hold</span> <span style="color: #ff0000;">off</span>;

It is quite common to plot the raw data as circular markers and the fitted data as a red dotted line. We can amend the above to do this:

<span style="color: #3366ff;">figure(</span>2<span style="color: #3366ff;">)</span>
<span style="color: #3366ff;">plot(</span><span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<span style="color: #800080;">'<span style="color: #ff9900;">o</span>'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerSize</span><span style="color: #800080;">'</span>,8,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerEdge</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">b</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">MarkerFace</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">b</span><span style="color: #800080;">'</span><span style="color: #3366ff;">)</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">on</span>;
<span style="color: #3366ff;">grid</span> <span style="color: #ff0000;">minor</span>;
<span style="color: #3366ff;">set(</span>gca,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</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;">hold</span> <span style="color: #ff0000;">on</span>;
<span style="color: #3366ff;">plot(</span><span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">3</span><span style="color: #666699;">)</span>,<span style="color: #800080;">'<span style="color: #ff9900;">r:</span>'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>,5<span style="color: #3366ff;">)</span>;
<span style="color: #3366ff;">hold</span> <span style="color: #ff0000;">off</span>;


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

To fit a second order polynomial we used lines 6-20.

[code language="matlab"]

x=[0:2:20]';
y=0.1*x.^2+1.2*x+5;
rawdata(:,1)=x;
rawdata(:,2)=y;

[P2]=polyfit(rawdata(:,1),rawdata(:,2),2);

fitteddata(:,1)=[1:2:19]';
fitteddata(:,3)=polyval(P2,fitteddata(:,1))

figure(2)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,3),'r:','LineWidth',5);
hold off

[/code]

These can be copied and pasted:

[code language="matlab"]

x=[0:2:20]';
y=0.1*x.^2+1.2*x+5;
rawdata(:,1)=x;
rawdata(:,2)=y;

[P2]=polyfit(rawdata(:,1),rawdata(:,2),2);

fitteddata(:,1)=[1:2:19]';
fitteddata(:,3)=polyval(P2,fitteddata(:,1))

figure(2)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,3),'r:','LineWidth',5);
hold off

[P2]=polyfit(rawdata(:,1),rawdata(:,2),2);

fitteddata(:,1)=[1:2:19]';
fitteddata(:,3)=polyval(P2,fitteddata(:,1))

figure(2)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,3),'r:','LineWidth',5);
hold off

[/code]

Lines 22-36 are now a duplicate of the original code. We can now go ahead and modify lines 6-20 to fit the data to a first order polynomial and to plot in figure 1 opposed to figure 2.

Line 6 is changed from

<span style="color: #ff0000;">[</span>P<strong><span style="color: #ff00ff;">2</span></strong><span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span><span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<strong><span style="color: #ff00ff;">2</span></strong><span style="color: #3366ff;">)</span>;

to

<span style="color: #ff0000;">[</span>P<strong><span style="color: #ff00ff;">1</span></strong><span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span><span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<strong><span style="color: #ff00ff;">1</span></strong><span style="color: #3366ff;">)</span>;

Line 9 is changed from

<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">3</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">polyval(</span>P<strong><span style="color: #ff00ff;">2</span></strong>,<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span><span style="color: #3366ff;">)</span>

to

<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">2</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">polyval(</span>P<strong><span style="color: #ff00ff;">1</span></strong>,<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span><span style="color: #3366ff;">)</span>

Line 11 is changed from

<span style="color: #3366ff;">figure(</span>2<span style="color: #3366ff;">)</span>

to

<span style="color: #3366ff;">figure(</span>1<span style="color: #3366ff;">)</span>

Line 19 is changed from

<span style="color: #3366ff;">plot(</span><span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">3</span></strong>),<span style="color: #800080;">'</span><span style="color: #ff9900;">r:</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>,5<span style="color: #3366ff;">)</span>;

to

<span style="color: #3366ff;">plot(</span><span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">2</span></strong>),<span style="color: #800080;">'</span><span style="color: #ff9900;">r:</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>,5<span style="color: #3366ff;">)</span>;

Here, in contrast to the second order polynomial fit we can immediately see that the 1st order polynomial linear fit does not follow all the data points.

\displaystyle \text{P1=}\left[ {\begin{array}{*{20}{c}} {3.20000} & {-1.00000} \end{array}} \right]

P1(1)

is the 

x

 coefficient 

P1(2)

is the constant.


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

Once again we can copy and paste lines 6-20. This time to lines 38-52. In order to fit a third order polynomial we will need to amend the following.

Line 38 is changed from

<span style="color: #ff0000;">[</span>P<strong><span style="color: #ff00ff;">2</span></strong><span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span><span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<strong><span style="color: #ff00ff;">2</span></strong><span style="color: #3366ff;">)</span>;

to

<span style="color: #ff0000;">[</span>P<strong><span style="color: #ff00ff;">3</span></strong><span style="color: #ff0000;">]</span>=<span style="color: #3366ff;">polyfit(</span><span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">rawdata(</span>:,<span style="color: #ff0000;">2</span><span style="color: #666699;">)</span>,<strong><span style="color: #ff00ff;">3</span></strong><span style="color: #3366ff;">)</span>;

Line 41 is changed from

<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">3</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">polyval(</span>P<strong><span style="color: #ff00ff;">2</span></strong>,<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span><span style="color: #3366ff;">)</span>

to

<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">4</span></strong><span style="color: #666699;">)</span>=<span style="color: #3366ff;">polyval(</span>P<strong><span style="color: #ff00ff;">3</span></strong>,<span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span><span style="color: #3366ff;">)</span>

Line 43 is changed from

<span style="color: #3366ff;">figure(</span>2<span style="color: #3366ff;">)</span>

to

<span style="color: #3366ff;">figure(</span>3<span style="color: #3366ff;">)</span>

Line 51 is changed from

<span style="color: #3366ff;">plot(</span><span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">3</span></strong>),<span style="color: #800080;">'</span><span style="color: #ff9900;">r:</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>,5<span style="color: #3366ff;">)</span>;

to

<span style="color: #3366ff;">plot(</span><span style="color: #666699;">fitteddata(</span>:,<span style="color: #ff0000;">1</span><span style="color: #666699;">)</span>,<span style="color: #666699;">fitteddata(</span>:,<strong><span style="color: #ff0000;">4</span></strong>),<span style="color: #800080;">'</span><span style="color: #ff9900;">r:</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">LineWidth</span><span style="color: #800080;">'</span>,5<span style="color: #3366ff;">)</span>;

This time the data is overfitted and hence follows the data points very closely

\displaystyle \text{P3=}\left[ {\begin{array}{*{20}{c}} {7.5775\text{e}-\text{18}} & {1.0000\text{e}-\text{01}} & {\text{1}\text{.2000e}+\text{00}} & {\text{5}\text{.0000e}+\text{00}} \end{array}} \right]

P3(1)

is the 

x.^3

 coefficient, 

P3(2)

is the 

x.^2

 coefficient 

P3(3)

is the 

x

 coefficient and

P3(4)

 is the constant.

In this case we have overfitted a model quadratic 2nd order polynomial data to a cubic function and as a consequence the coefficient of the additional parameter 

P3(1)≈0

 i.e. we can say this additional parameter is not required to fit this data.

Subplots

It is perhaps useful to plot the linear, quadratic and cubic fits together in one figure using subplots. It may also be useful to print the equation on the graphs. To do this we can rearrange the code, so all the graphing commands are at the bottom

[code language="matlab"]

x=[0:2:20]';
y=0.1*x.^2+1.2*x+5;
rawdata(:,1)=x;
rawdata(:,2)=y;

[P1]=polyfit(rawdata(:,1),rawdata(:,2),1);
fitteddata(:,1)=[1:2:19]';
fitteddata(:,2)=polyval(P1,fitteddata(:,1))

[P2]=polyfit(rawdata(:,1),rawdata(:,2),2);
fitteddata(:,1)=[1:2:19]';
fitteddata(:,3)=polyval(P2,fitteddata(:,1))

[P3]=polyfit(rawdata(:,1),rawdata(:,2),3);
fitteddata(:,1)=[1:2:19]';
fitteddata(:,4)=polyval(P3,fitteddata(:,1))

figure(1)

plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,2),'r:','LineWidth',5);

hold off

figure(2)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,3),'r:','LineWidth',5);

hold off

figure(3)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,4),'r:','LineWidth',5);

hold off

[/code]

To modify the code above to make a subplot I'm going to add Line 19:

<span style="color: #3366ff;">subplot(</span>3,1,1<span style="color: #3366ff;">)</span>

Replace Line 31 with

<span style="color: #3366ff;">subplot(</span>3,1,2<span style="color: #3366ff;">)</span>

Replace Line 43 with

<span style="color: #3366ff;">subplot(</span>3,1,3<span style="color: #3366ff;">)</span>

A few lines will need to be added to print the equation on each subplot. The linear equation looks like

\displaystyle y=\text{P1(1)}x+\text{P1(2)}

We will need to put together the equation as a string. For this we use

<span style="color: #ff0000;">[ ]</span>

to make an array and we type the equation as a string enclosed in

<span style="color: #800080;">' '</span>

. Opposed to typing in the coefficients we want to read them from P1. We can use these as input arguments for the 

<span style="color: #3366ff;">num2str</span>

function, this function will convert these inputs into strings.

FitEqnP1=['y=' num2str(P1(1

<span style="color: #666699;">)</span><span style="color: #3366ff;">)</span> <span style="color: #800080;">'</span><span style="color: #ff9900;">x +</span><span style="color: #800080;">'</span> <span style="color: #3366ff;">num2str(</span><span style="color: #666699;">P1(</span>2<span style="color: #666699;">)</span><span style="color: #3366ff;">)</span><span style="color: #ff0000;">]</span>

Now we can create a label in the form of a cell array using the string variable FitEqnP1. The

<span style="color: #800000;">{</span> <span style="color: #800000;">}</span>

are required as the strings of text in each row are different sizes.

labelP1=<span style="color: #800000;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Fitted Equation:</span><span style="color: #800080;">'</span>; FitEqnP1<span style="color: #800000;">}</span>

Now we can use the function text, to add the label to the graph

<span style="color: #3366ff;">text(</span>x,y,stringarray<span style="color: #3366ff;">)</span>

Here x and y are the co-ordinates on the figure, in this case I will use x=2 and y=60. I'll use the labelP1 and add two additional input arguments to increase the Font Size to 20.

<span style="color: #3366ff;">text(</span>2,60,labelP1,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</span>

The quadratic equation looks like

\displaystyle y=\text{P2(1)}{{x}^{2}}+\text{P2(2)}x+\text{P2(3)}

We will once again need to put together the equation as a string. For this we use

<span style="color: #ff0000;">[ ]</span>

to make an array and we type the equation as a string enclosed in

<span style="color: #800080;">' '</span>

. Opposed to typing in the coefficients we want to read them from P2. We can use these as input arguments for the 

<span style="color: #3366ff;">num2str</span>

function, this function will convert these inputs into strings.

FitEqnP2=['y=' num2str(P2(1

<span style="color: #666699;">)</span><span style="color: #3366ff;">)</span> <span style="color: #800080;">'</span><span style="color: #ff9900;">x^2 +</span><span style="color: #800080;">'</span> <span style="color: #3366ff;">num2str(</span><span style="color: #666699;">P2(</span>2<span style="color: #666699;">)</span><span style="color: #3366ff;">) <span style="color: #800080;">'</span><span style="color: #ff9900;">x +</span><span style="color: #800080;">'</span> num2str(<span style="color: #666699;">P2(</span><span style="color: #000000;">3</span><span style="color: #666699;">)</span>)</span><span style="color: #ff0000;">]</span>

Now we can create a label in the form of a cell array using the string variable FitEqnP2. The

<span style="color: #800000;">{ }</span>

are required as the strings of text in each row are different sizes.

labelP2=<span style="color: #800000;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Fitted Equation:</span><span style="color: #800080;">'</span>; FitEqnP2<span style="color: #800000;">}</span>

Now we can use the function text, to add the label to the graph

<span style="color: #3366ff;">text(</span>x,y,stringarray<span style="color: #3366ff;">)</span>

Here x and y are the co-ordinates on the figure, in this case I will use x=2 and y=60. I'll use the labelP2 and add two additional input arguments to increase the Font Size to 20.

<span style="color: #3366ff;">text(</span>2,60,labelP2,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</span>

The cubic equation looks like

\displaystyle y=\text{P3(1)}{{x}^{3}}\text{+P3(2)}{{x}^{2}}+\text{P3(3)}x+\text{P3(4)}

We will once again need to put together the equation as a string. For this we use

<span style="color: #ff0000;">[ ]</span>

to make an array and we type the equation as a string enclosed in

<span style="color: #800080;">' '</span>

. Opposed to typing in the coefficients we want to read them from P3. We can use these as input arguments for the 

<span style="color: #3366ff;">num2str</span>

function, this function will convert these inputs into strings.

FitEqnP3=['y=' num2str(P3(1)) 'x^3 +' num2str(P3(2

<span style="color: #666699;">)</span><span style="color: #3366ff;">)</span> <span style="color: #800080;">'</span><span style="color: #ff9900;">x^2 +</span><span style="color: #800080;">'</span> <span style="color: #3366ff;">num2str(</span><span style="color: #666699;">P3(</span>3<span style="color: #666699;">)</span><span style="color: #3366ff;">) <span style="color: #800080;">'</span><span style="color: #ff9900;">x +</span><span style="color: #800080;">'</span> num2str(<span style="color: #666699;">P3(</span><span style="color: #000000;">4</span><span style="color: #666699;">)</span>)</span><span style="color: #ff0000;">]</span>

Now we can create a label in the form of a cell array using the string variable FitEqnP2. The

<span style="color: #800000;">{ }</span>

are required as the strings of text in each row are different sizes.

labelP3=<span style="color: #800000;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Fitted Equation:</span><span style="color: #800080;">'</span>; FitEqnP3<span style="color: #800000;">}</span>

Now we can use the function text, to add the label to the graph

<span style="color: #3366ff;">text(</span>x,y,stringarray<span style="color: #3366ff;">)</span>

Here x and y are the co-ordinates on the figure, in this case I will use x=2 and y=60. I'll use the labelP2 and add two additional input arguments to increase the Font Size to 20.

<span style="color: #3366ff;">text(</span>2,60,labelP2,<span style="color: #800080;">'</span><span style="color: #ff9900;">FontSize</span><span style="color: #800080;">'</span>,20<span style="color: #3366ff;">)</span>

When ran this gives the subplot with the equations

[code language="matlab"]

x=[0:2:20]';
y=0.1*x.^2+1.2*x+5;
rawdata(:,1)=x;
rawdata(:,2)=y;

[P1]=polyfit(rawdata(:,1),rawdata(:,2),1);
fitteddata(:,1)=[1:2:19]';
fitteddata(:,2)=polyval(P1,fitteddata(:,1))

[P2]=polyfit(rawdata(:,1),rawdata(:,2),2);
fitteddata(:,1)=[1:2:19]';
fitteddata(:,3)=polyval(P2,fitteddata(:,1))

[P3]=polyfit(rawdata(:,1),rawdata(:,2),3);
fitteddata(:,1)=[1:2:19]';
fitteddata(:,4)=polyval(P3,fitteddata(:,1))

figure(1)
subplot(3,1,1)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,2),'r:','LineWidth',5);
FitEqnP1=['y=' num2str(P1(1)) 'x +' num2str(P1(2))]
labelP1={'Fitted Equation:'; FitEqnP1}
text(2,60,labelP1,'FontSize',20)
hold off

subplot(3,1,2)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,3),'r:','LineWidth',5);
FitEqnP2=['y=' num2str(P2(1)) 'x^2 +' num2str(P2(2)) 'x +' num2str(P2(3))]
labelP2={'Fitted Equation:'; FitEqnP2}
text(2,60,labelP2,'FontSize',20)
hold off

subplot(3,1,3)
plot(rawdata(:,1),rawdata(:,2),'o','MarkerSize',8,'MarkerEdge','b','MarkerFace','b');
grid on;
grid minor;
set(gca,'FontSize',20);
xlabel('x');
ylabel('y');
hold on
plot(fitteddata(:,1),fitteddata(:,4),'r:','LineWidth',5);
FitEqnP3=['y=' num2str(P3(1)) 'x^3 +' num2str(P3(2)) 'x^2 +' num2str(P3(3)) 'x +' num2str(P3(4))]
labelP3={'Fitted Equation:'; FitEqnP3}
text(2,60,labelP2,'FontSize',20)
hold off

[/code]