Linear Fit and Axes Labeling
조회 수: 8 (최근 30일)
이전 댓글 표시
I've created a scatter plot, but I also want the linear fit of this plot. Is there a command for this. Also, how can I label both axes and add a title to the plot?
댓글 수: 0
답변 (2개)
Christiaan
2015년 6월 22일
Dear Suyapa,
There are may possible ways to plot a linear fit in a scattered plot (with least squares for example). This is also called linear regression. Therefore in MATLAB you can use one of the linear regression tools.
Documentation can be found here: http://www.mathworks.com/help/stats/linear-regression-model-workflow.html
You can have a look at the examples, and then I am sure you find one that suits your problem.
Kind regards, Christiaan
댓글 수: 0
Star Strider
2015년 6월 22일
There are several ways to do what you want. If you have the Statistics Toolbox, consider the lsline function.
Otherwise, consider polyfit (and polyval) with a first-degree polynomial:
b = polyfit(x, y, 1);
yf = polyval(b, x);
then plot it as:
figure(1)
scatter(x, y)
hold on
plot(x, yf)
hold off
title('Scatterplot of (x,y) data')
xlabel('x data')
ylabel('y data')
This works best if your x-data are ordered.
See the documentation for the plot function for other options.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!