How to plot best fit line?

조회 수: 3,026 (최근 30일)
fzhmktr
fzhmktr 2018년 1월 15일
댓글: Miles 2023년 11월 11일
I have 1700 plot of data in graph. How do I plot the line of best fit? I stored the x and y data in table and the plot them. From the graph, I can see that the graph plotting is upwards. I have read other answers for this kind of question but still confused. Please help. Thank you.
x = score;
y = MOS;
scatter(x,y)
xlabel('score')
ylabel('MOS')

채택된 답변

Image Analyst
Image Analyst 2018년 1월 15일
편집: Image Analyst 2021년 2월 10일
Use polyfit() and polyval():
% Get coefficients of a line fit through the data.
coefficients = polyfit(x, y, 1);
% Create a new x axis with exactly 1000 points (or whatever you want).
xFit = linspace(min(x), max(x), 1000);
% Get the estimated yFit value for each of those 1000 new x locations.
yFit = polyval(coefficients , xFit);
% Plot everything.
plot(x, y, 'b.', 'MarkerSize', 15); % Plot training data.
hold on; % Set hold on so the next plot does not blow away the one we just drew.
plot(xFit, yFit, 'r-', 'LineWidth', 2); % Plot fitted line.
grid on;
See attached for a full demo.
  댓글 수: 3
Sahil Bharti
Sahil Bharti 2021년 7월 22일
Thanks. it was usefull to me as well
Miles
Miles 2023년 11월 11일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jos (10584)
Jos (10584) 2018년 1월 15일
if it is just for plotting, the command lsline would do.
x = 1:10 ; y = 2* x + 3 ;
plot(x, y, 'bo') ;
lsline
  댓글 수: 1
optoengineer2
optoengineer2 2021년 2월 9일
lsline works only with "Statistics and Machine Learning Toolbox"

댓글을 달려면 로그인하십시오.


Veer Gatha
Veer Gatha 2021년 5월 18일
x = {1;3;5;7;8};
y = {1;4;6;8;0};
scatter(x,y)
xlabel('score')
ylabel('MOS')

Ravisha Gunawardhana
Ravisha Gunawardhana 2022년 10월 12일
This command doesn't work, You have to put square brackets for the graph to be plotted otherwise an error occurs.
  댓글 수: 1
Image Analyst
Image Analyst 2022년 10월 12일
@Ravisha Gunawardhana What command? There is lots of code and commands shown above. Who are you replying to? Are you commenting on someone's answer, or answering the original poster, @fzhmktr?
Where do you say someone needs square brackets? If the original poster is right that x and y are table variables then I think he'll have to use a dot to get the particular column he wants to plot since scatter() can't take a table variable as an input.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Support Vector Machine Regression에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by