When plotting a scatter plot is it possible to add a linear fit to the the graph without having to go into tools-> basic fitting and clicking on linear and show equations?

 채택된 답변

Wayne King
Wayne King 2012년 2월 1일

11 개 추천

lsline is in the Statistics Toolbox, if you do not have that product you can use polyfit() to fit a 1st order polynomial.
x = 1:10;
y1 = x + randn(1,10);
scatter(x,y1,25,'b','*')
P = polyfit(x,y1,1);
yfit = P(1)*x+P(2);
hold on;
plot(x,yfit,'r-.');

댓글 수: 5

Richard
Richard 2012년 2월 1일
great that works. And what about obtaining the equation for the straight line: y=mx+c?
Wayne King
Wayne King 2012년 2월 1일
yfit = P(1)*x+P(2)
is the equation.... P(1) is the slope and P(2) is the intercept
Nikolaus Koopmann
Nikolaus Koopmann 2020년 6월 3일
yfit=@(x)P(1)*x+P(2)
is what he means, i think
Galina Machavariani
Galina Machavariani 2021년 9월 2일
Thank you for the answer.
And how can I display linear fit equation on the graph?
I mean not "manual" way in the graph editor, , but the code ...
Thank you !
You would need to create the string of the equation and then place it on the graph with "text". Here is an expanded version of Wayne's example that does this:
x = 1:10;
y1 = x + randn(1,10);
scatter(x,y1,25,'b','*')
P = polyfit(x,y1,1);
yfit = polyval(P,x);
hold on;
plot(x,yfit,'r-.');
eqn = string(" Linear: y = " + P(1)) + "x + " + string(P(2));
text(min(x),max(y1),eqn,"HorizontalAlignment","left","VerticalAlignment","top")

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

추가 답변 (5개)

Thomas
Thomas 2012년 1월 31일

1 개 추천

Also you can always do it once manually, generate data set, create the plot, make the linear fit with the equations, then in the Figure window
File>Generate code..
This will create a MATLAB function for everything that you did manually and can use it again and again if you have more data sets.

댓글 수: 1

Galina Machavariani
Galina Machavariani 2021년 9월 2일
After I did linear fit with equation, What should I write in the command window to generate the code?

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

Wayne King
Wayne King 2012년 1월 31일

0 개 추천

Hi, yes, you can use lsline()
x = 1:10;
y1 = x + randn(1,10);
scatter(x,y1,25,'b','*')
hold on;
lsline;

댓글 수: 4

Richard
Richard 2012년 1월 31일
matlab doesnt seem to recognise isline! Am I missing a toolbox, the version I'm working with is 2011b?
Wayne King
Wayne King 2012년 1월 31일
lsline(), not isline. Did you copy and paste my code? I didn't write isline.
Richard
Richard 2012년 2월 1일
>> clear all
>> x = 1:10;
y1 = x + randn(1,10);
scatter(x,y1,25,'b','*')
hold on;
lsline;
Undefined function or variable 'lsline'.
vkehayas
vkehayas 2016년 9월 30일
The statistics toolbox is required for
lsline

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

Annu Panwar
Annu Panwar 2017년 9월 13일

0 개 추천

but anyone has observed that the results are different when you do polyfit by using codes and manually?

댓글 수: 2

Danhay
Danhay 2018년 3월 6일
that is true. I observed that too
Namira
Namira 2018년 8월 11일
I observed that too. Do you know the solution?

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

sabreen haj
sabreen haj 2018년 4월 27일

0 개 추천

Can you help me to write script for calibration curve And give me the equation so i can finde the x value then the result shown in a table with everage of 3 x value and std
Marcello Wienhoven
Marcello Wienhoven 2021년 1월 11일

0 개 추천

x = 1:10;
y1 = x + randn(1,10);
scatter(x,y1,25,'b','*')
P = polyfit(x,y1,1);
yfit = P(1)*x+P(2);
hold on;
plot(x,yfit,'r-.');

댓글 수: 1

Galina Machavariani
Galina Machavariani 2021년 9월 2일
Thank you for the answer.
And how can I display linear fit equation on the graph?
I mean not "manual" way in the graph editor, , but the code ...
Thank you !

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

태그

질문:

2012년 1월 31일

댓글:

2022년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by