Effacer les filtres
Effacer les filtres

linear fit

1 373 vues (au cours des 30 derniers jours)
Richard
Richard le 31 Jan 2012
Commenté : Seth DeLand le 25 Mai 2022
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?

Réponse acceptée

Wayne King
Wayne King le 1 Fév 2012
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 commentaires
Galina Machavariani
Galina Machavariani le 2 Sep 2021
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 le 25 Mai 2022
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")

Connectez-vous pour commenter.

Plus de réponses (5)

Thomas
Thomas le 31 Jan 2012
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 commentaire
Galina Machavariani
Galina Machavariani le 2 Sep 2021
After I did linear fit with equation, What should I write in the command window to generate the code?

Connectez-vous pour commenter.


Wayne King
Wayne King le 31 Jan 2012
Hi, yes, you can use lsline()
x = 1:10;
y1 = x + randn(1,10);
scatter(x,y1,25,'b','*')
hold on;
lsline;
  4 commentaires
Richard
Richard le 1 Fév 2012
>> 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 le 30 Sep 2016
The statistics toolbox is required for
lsline

Connectez-vous pour commenter.


Annu Panwar
Annu Panwar le 13 Sep 2017
but anyone has observed that the results are different when you do polyfit by using codes and manually?
  2 commentaires
Danhay
Danhay le 6 Mar 2018
that is true. I observed that too
Namira
Namira le 11 Août 2018
I observed that too. Do you know the solution?

Connectez-vous pour commenter.


sabreen haj
sabreen haj le 27 Avr 2018
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 le 11 Jan 2021
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 commentaire
Galina Machavariani
Galina Machavariani le 2 Sep 2021
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 !

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by