What code do I use to add a regression (least squares) line to an existing scatter graph?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have created scatter graphs in Matlab using the plot(x,y) function, but how do I now add regression lines to them?
0 comentarios
Respuestas (2)
Star Strider
el 28 de Nov. de 2015
You can use polyfit and polyval (or a number of other regression options if you have the Statistics Toolbox), and using variations on this simple two-parameter linear regression:
B = [ones(size(x(:)) x(:)]\y(:);
y_fit = [ones(size(x(:)) x(:)]*B;
Here ‘B’ are the estimated parameters and ‘y_fit’ is the fitted regression line. To plot them (assuming your variables are sorted with ‘x’ as either ascending or descending)
figure(1)
scatter(x, y)
hold on
plot(x, y_fit)
hold off
grid
0 comentarios
Image Analyst
el 28 de Nov. de 2015
See my attached demo on polyfit below this image it creates.

Feel free to adapt as needed.
0 comentarios
Ver también
Categorías
Más información sobre Fit Postprocessing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!