Effacer les filtres
Effacer les filtres

Producing a line of best fit with equation

112 vues (au cours des 30 derniers jours)
badfish66
badfish66 le 24 Avr 2016
Commenté : Image Analyst le 10 Déc 2021
Hi
I have been using lsline to produce a linear line of bext fit for two datasets. I was wondering if there was a similar command that produced the line of best fit and provided an equation (in the form y=mx+c) for said line.
Many Thanks

Réponse acceptée

Sebastian Castro
Sebastian Castro le 24 Avr 2016
There is also the polyfit function in MATLAB. If you specify an order of 1, the output of polyfit will have the two components m and c as you specified.
- Sebastian

Plus de réponses (2)

Image Analyst
Image Analyst le 24 Avr 2016
See attached demo of polyfit.

Kylie Hansen
Kylie Hansen le 16 Fév 2017
Modifié(e) : Image Analyst le 10 Déc 2021
Thought I'd share an alternate answer from this github repository:
The code below prints a 1x2 matrix where the first value is the slope of the line and the second is the y-int.
Just plug into slope intercept form (y = mx+ b) and you've got the equation.
h = lsline ;
p2 = polyfit(get(h,'xdata'),get(h,'ydata'),1)
  2 commentaires
Ryan Kunen
Ryan Kunen le 10 Déc 2021
h = lsline ;
p2 = polyfit(get(h,'xdata'),get(h,'ydata'),1)
Image Analyst
Image Analyst le 10 Déc 2021
@Ryan Kunen, this is just the code @Kylie Hansen gave. Did you forget to add some comment about it?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by