linear fit

1,559 ビュー (過去 30 日間)
Richard
Richard 2012 年 1 月 31 日
コメント済み: Seth DeLand 2022 年 5 月 25 日
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 日
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 件のコメント
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 !
Seth DeLand
Seth DeLand 2022 年 5 月 25 日
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 日
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 日
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 年 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 日
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 日
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 日
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 !

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by