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?

 Respuesta aceptada

Wayne King
Wayne King el 1 de Feb. de 2012

11 votos

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 comentarios

Richard
Richard el 1 de Feb. de 2012
great that works. And what about obtaining the equation for the straight line: y=mx+c?
Wayne King
Wayne King el 1 de Feb. de 2012
yfit = P(1)*x+P(2)
is the equation.... P(1) is the slope and P(2) is the intercept
Nikolaus Koopmann
Nikolaus Koopmann el 3 de Jun. de 2020
yfit=@(x)P(1)*x+P(2)
is what he means, i think
Galina Machavariani
Galina Machavariani el 2 de Sept. de 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 el 25 de Mayo de 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")

Iniciar sesión para comentar.

Más respuestas (5)

Thomas
Thomas el 31 de En. de 2012

1 voto

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 comentario

Galina Machavariani
Galina Machavariani el 2 de Sept. de 2021
After I did linear fit with equation, What should I write in the command window to generate the code?

Iniciar sesión para comentar.

Wayne King
Wayne King el 31 de En. de 2012

0 votos

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

4 comentarios

Richard
Richard el 31 de En. de 2012
matlab doesnt seem to recognise isline! Am I missing a toolbox, the version I'm working with is 2011b?
Wayne King
Wayne King el 31 de En. de 2012
lsline(), not isline. Did you copy and paste my code? I didn't write isline.
Richard
Richard el 1 de Feb. de 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 el 30 de Sept. de 2016
The statistics toolbox is required for
lsline

Iniciar sesión para comentar.

Annu Panwar
Annu Panwar el 13 de Sept. de 2017

0 votos

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

2 comentarios

Danhay
Danhay el 6 de Mzo. de 2018
that is true. I observed that too
Namira
Namira el 11 de Ag. de 2018
I observed that too. Do you know the solution?

Iniciar sesión para comentar.

sabreen haj
sabreen haj el 27 de Abr. de 2018

0 votos

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 el 11 de En. de 2021

0 votos

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 comentario

Galina Machavariani
Galina Machavariani el 2 de Sept. de 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 !

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 31 de En. de 2012

Comentada:

el 25 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by