ELEC 242 Lab

Fitting a Line to Data Points

A quick way to fit a line (or any polynomial) to a set of data points is with the polyfit() function of Matlab. If you've already used Matlab to plot the data, skip to Step 3.


Step 1:

Start matlab by double clicking on the matlab icon on the desktop.

Step 2:

Enter the x and y values as vectors.

>> x=[1 2 3 4 5 6];
>> y=[2.1 4.1 6.1 8.1 10.1 12.1];

Step 3:

Type "polyfit(x,y,1)".

Step 4:

The result is the slope and y-intercept:

ans =
    2.0000    0.1000