Time for action - using polyfit
1. Assume that we have loaded the relevant data into Octave and stored the leaf lengths of tree A and B in variables
yA
andyB
. The corresponding heights are stored inxA
andxB
.2. To fit the linear model to the data in
yA
, we do the following:
octave:15> [cA sA] = polyfit(xA, yA, 1);
3. We must check if the fit made any sense at all. First, we can plot the resulting linear fit together with the data:
octave:16> plot(xA, yA, 'rs', xA, sA.yf, 'r');
and is shown in the figure below with red squares and a red line.
4. The fit of y B to the third order polynomial follows the same procedure:
octave:17> [cB sB] = polyfit(xB, yB, 3); octave:18> hold on; plot(xB, yB, 'bv', xB, sB.yf, 'b');
The plot is shown in the figure below:
Note
Notice that I have used the plotting style format'rs'
and'bv'
. Depending on your plotting backend, this may not be supported. You can change it to, for example,'ro'
and'b+'
What just happened?
polyfit
finds the polynomial coefficients...