How do I make a best fit line? along with getting R^2 on matlab.

28 ビュー (過去 30 日間)
nicholas m.
nicholas m. 2013 年 10 月 6 日
コメント済み: 10B 2016 年 2 月 19 日
I am trying to make a best fit line out of a set of data for my physics class, but I don't know how to enter a table of 7 (x) and (y) values into matlab. I also don't know how to plot those data points and get a best fit linear line with them. I was told you don't use the fplot command. Also I need the coefficient of determination (R^2).

採用された回答

Image Analyst
Image Analyst 2013 年 10 月 6 日
To enter the values into MATLAB, you can list all 14 values (7 values for x and 7 values for y) in between brackets:
x = [1,3,4,7,13,19,42];
y = [2,4,6,8,11,36,52];
Then call plot()
plot(x, y, 'b*-', 'LineWidth', 2, 'MarkerSize', 15);
Then get a fit
coeffs = polyfit(x, y, 1);
% Get fitted values
fittedX = linspace(min(x), max(x), 200);
fittedY = polyval(coeffs, fittedX);
% Plot the fitted line
hold on;
plot(fittedX, fittedY, 'r-', 'LineWidth', 3);
  5 件のコメント
Daniel Shub
Daniel Shub 2013 年 10 月 7 日
You are correct. The LSLINE function is part of the stats toolbox. I didn't realize that. That said the student edition comes with the stats toolbox. I don't use LSLINE much, but I find it to be pretty convenient when I do.
10B
10B 2016 年 2 月 19 日
Good answer Image Analyst...

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2013 年 10 月 6 日
I think the simplest way to do what you want is to use the polyfit() command. You can type
doc polyfit
at the command line to see documentation (and a simple example that you should be able to mimic). The documentation is also here:
  1 件のコメント
Daniel Shub
Daniel Shub 2013 年 10 月 6 日
Since you don't need the coefficients of the line to get R^2, I think lsline is even easier for plotting the line.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by