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?
Asked
Active
Viewed 5.9k times
2 Answers
2
Use polyfit(x,y,1) to get the coefficients for a linear fit. Use polyval(polyfit(x,y,1),x) to get the fitted y-values for your desired x values. Then you can plot your x and your polvals to form the line.
If you already have a scatter plot, and are only using linear fits, I would do:
// scatterplot above
hold on;
coef_fit = polyfit(x,y,1);
y_fit = polyval(coef_fit,xlim);
plot(xlim,y_fit,'r');
hold off;

user3685285
- 6,066
- 13
- 54
- 95
-
1How is your answer different than mine? – Benoit_11 Aug 27 '14 at 20:04