How should I add a line of best fit to my plot without the statistics package?

user2998603 picture user2998603 · Aug 27, 2014 · Viewed 58.2k times · Source

I have a scatter plot of data and I want to add a best fit line. All I can find online is the statistics package, but my school hasn't paid for that. Is there another was to do this - without the statistics package?

Answer

Benoit_11 picture Benoit_11 · Aug 27, 2014

You can use polyfit to obtain a 1st order polynomial which best fits your data.

Eg:

Fit = polyfit(x,y,1); % x = x data, y = y data, 1 = order of the polynomial.

You can plot the line along with your scatter plot with polyval:

plot(polyval(Fit,x))

Hope that helps!