How to do linear regression?
Mostrar comentarios más antiguos
How to do a regression line ?
1 comentario
Stephan
el 22 de Ag. de 2018
Do you have access to Optimization Toolbox or to Curve Fitting Toolbox?
Respuestas (2)
Star Strider
el 22 de Ag. de 2018
1 voto
5 comentarios
Stephan
el 22 de Ag. de 2018
Note that this solution only gives a correct answer, when the function passes through the origin of the coordinate system
y= m .* x + n ---> with n = 0
will give a correct answer. If n~=0 you get more bad results for m the more n is different from zero.
Star Strider
el 22 de Ag. de 2018
It gives a correct result if you include a column vector of ones with the independent variable to estimate the intercept:
B = [x(:) ones(size(x(:)))] \ y(:); % Estimate Parameters
yfit = [x(:) ones(size(x(:)))] * B; % Calculate Regression Line
figure
plot(x, y, 'pg')
hold on
plot(x, yfit, '-r')
hold off
grid
Stephan
el 22 de Ag. de 2018
Nice,
and learned something again ;-)
Star Strider
el 22 de Ag. de 2018
Thank you!
Actually, I got the idea for the column of ones from the documentation for the regress (link) function when I first began using it, many years ago. (I simply forgot to include polyfit and friends initially.)
Stephan
el 22 de Ag. de 2018
I had to think about it for a few minutes. But after understanding what happens, i saw that this is a pretty nice approach.
Image Analyst
el 22 de Ag. de 2018
0 votos
See my attached demo.

Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!