Why do these two different way to find slope give different results?

2 ビュー (過去 30 日間)
Luqman Saleem
Luqman Saleem 2019 年 5 月 20 日
コメント済み: Luqman Saleem 2019 年 5 月 21 日
I have the following data
x = [14 18 22 26];
y = [8.5588 14.3430 21.6132 30.3740];
and I want to find the slope of this data when it is plotted on a loglog() plot. I tried two methods:
First method:
loglog(x,y);
then go to tools > basic fitting > linear fitting
the result is
y = p1*x + p2
Coefficients:
p1 = 1.8179
p2 = -17.636
Norm of residuals =
1.4883
slope = 1.8179
Second method:
coeffs = polyfit(log(x),log(y),1);
slope = coeffs(1);
by this method
slope = 2.0462
Why is it so? The slope that I find by using first method is closer to the experimental results. Is there any way to do the first method through code instead of GUI?
  2 件のコメント
Luqman Saleem
Luqman Saleem 2019 年 5 月 20 日
thank you for a quick response. is there anyway to access "best fitting" GUI's resutls through command line? I have a big data and can't do fitting one by one.
Adam Danz
Adam Danz 2019 年 5 月 20 日
Sorry, I removed my comment because it only pointed out the obvious. Check out my answer below and let me know if there are any more questions.

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

採用された回答

Adam Danz
Adam Danz 2019 年 5 月 20 日
編集済み: Adam Danz 2019 年 5 月 21 日
The basic fitting tool merely calls polyfit() using your x and y values (see link for more info).
You can get the same coefficients by calling polyfit directly.
x = [14 18 22 26];
y = [8.5588 14.3430 21.6132 30.3740];
coefs = polyfit(x,y,1);
%coefs = [1.8179 -17.636] % Same results as using fitting tool.
Then you can add a reference line by using refline().
hold on
rh = refline(coefs(1),coefs(2));
rh.Color = 'r';
As you'll see after plotting the reference line, it does not align with your data in log space.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by