Line of Best Fit through Scattered Data
Show older comments
I need to find the line of best fit through my scatterplot. I have attached my text file, and my code is the following.
clear all
fid = fopen( 'oo20.txt');
data = textscan(fid, '%f%f', 'Delimiter', '|', 'TreatAsEmpty','~');
fclose(fid);
GalList.year = data{1};
D = data{2};
X1 = GalList.year;
Y1 = D;
scatter(X1,Y1);
ylim([0 20])
3 Comments
Star Strider
on 11 Oct 2015
Edited: Star Strider
on 11 Oct 2015
While we’re waiting for the text file data to magickally appear, does the ‘line of best fit’ have any constraints or preferences? Do you have a model function that you want to fit a nonlinear regression? Is there anything (other than a line on a plot) you want to get from the regression analysis? (Note that my background is not in deep-space astronomy, so assume I know nothing about doing regressions on galaxies.)
Explaining X1 and Y1 are would help too, including their physical significance and if the fitted regression line has any physical significance, or is just for fun!
jgillis16
on 11 Oct 2015
Star Strider
on 11 Oct 2015
I just peeked at it and it seems reasonable to delete this one:
~|3.4028886
Does it have any specific significance?
Accepted Answer
More Answers (2)
Image Analyst
on 11 Oct 2015
0 votes
There are other more sophisticated methods, but try polyfit() and polyval(). See attached demo.
The following attempts to fit the data to an equation A*X1+B*Y1=C,
X1=X1(:);
Y1=Y1(:);
e=-ones(length(X1),1);
[~,~,V]=svd( [X1,Y1,e], 0);
ABC=V(:,end);
A=ABC(1);
B=ABC(2);
C=ABC(3);
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!