I am trying to determine the slope of the best-fit line in log space, and plot the best-fit line as a visual check. It needs to be a line, not a curve (I understand that the misfits could be very large in logspace). Below is an example with xy data and polyfit attempts (and plot included). Thanks for any help
x = [7.94, 16.23, 32.92, 66.8, 135.52, 274.93, 557.78, 1131.59, 2295.72, 4657.46];
y = [134000, 102000, 31000, 11000, 2600, 990, 40, 10.41, 3.48, 1.037];
scatter(x,y, 'DisplayName', 'MyData')
set(gca,'xscale','log')
set(gca,'yscale','log')
hold on
grid on
box on
axis equal
p = polyfit(log10(x), log10(y), 1);
z = polyval(p, log10(x));
loglog(x, log10(z), 'DisplayName', 'Try1');
loglog(x, z, 'DisplayName', 'Try2');
z2 = polyval(p, x);
loglog(x, z2, 'DisplayName', 'Try3');
loglog(x, log10(z2), 'DisplayName', 'Try4');
legend
Capture.JPG

 採用された回答

Star Strider
Star Strider 2019 年 7 月 27 日

0 投票

You are regressing ‘log10(x)’ against ‘log10(y)’ so you need to give the appropriate information to both polyfit and polyval:
Bp = polyfit(log10(x), log10(y), 1);
Yp = polyval(Bp,log10(x));
figure
plot(log10(x), log10(y), 'pg')
hold on
plot(log10(x), Yp, '-r')
hold off
grid
producing this plot:
The slope is -2.0182.

4 件のコメント

newbie9
newbie9 2019 年 8 月 3 日
the fit needs to be a straight line in logspace, not linspace
Star Strider
Star Strider 2019 年 8 月 3 日
O.K., try this:
x = [7.94, 16.23, 32.92, 66.8, 135.52, 274.93, 557.78, 1131.59, 2295.72, 4657.46];
y = [134000, 102000, 31000, 11000, 2600, 990, 40, 10.41, 3.48, 1.037];
Bp = polyfit(log10(x), log10(y), 1);
Yp = polyval(Bp,log10(x));
figure
loglog(x, y, 'pg')
hold on
loglog(x, 10.^Yp, '-r')
hold off
grid
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)))) floor(log10(abs(x(:))))]; % Mantissa & Exponent
Bp2 = expstr(10^Bp(2));
eqn = sprintf('y = %.2f\\times10^{%d}\\cdotx^{%.2f}',Bp2(1), Bp2(2), Bp(1));
legend('Data', eqn)
Same essential code & plot, different axes scales:
Plot best fit line in log-space & get slope - 2019 08 03.png
Experiment to get it to look the way you want.
newbie9
newbie9 2019 年 8 月 3 日
thanks, that's what I have below
Star Strider
Star Strider 2019 年 8 月 3 日
As always, my pleasure.
I was working on it as you were posting.

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

その他の回答 (1 件)

newbie9
newbie9 2019 年 8 月 3 日

1 投票

Star Strider basically gave the correct answer, so I have accepted; but I wanted to show the fit in log-log space, so here's my version of code, considering Star Strider's answer, in case it is useful to anyone else:
x = [7.94, 16.23, 32.92, 66.8, 135.52, 274.93, 557.78, 1131.59, 2295.72, 4657.46];
y = [134000, 102000, 31000, 11000, 2600, 990, 40, 10.41, 3.48, 1.037];
scatter(x,y, 'DisplayName', 'MyData')
set(gca,'xscale','log')
set(gca,'yscale','log')
hold on
grid on
box on
axis equal
%
Bp = polyfit(log10(x), log10(y), 1);
Yp = polyval(Bp, log10(x));
Yp2 = 10.^(Yp);
%
plot(x, Yp2, '-r', 'DisplayName', 'Fit');
text(1.1, 10, strcat('Slope in Log-Log Space =', num2str(Bp(1))), 'Interpreter', 'none');
legend
Which yields this plot:
untitled.jpg

1 件のコメント

Angie Brulc
Angie Brulc 2022 年 3 月 7 日
Hello,
I'm a random student working on processing data for a lab and just wanted to let you know this was incredibly helpful to me. Thank you!

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

製品

リリース

R2018a

質問済み:

2019 年 7 月 27 日

コメント済み:

2022 年 3 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by