Plot best fit line in log-space & get slope
조회 수: 91 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Star Strider
2019년 7월 27일
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
추가 답변 (1개)
newbie9
2019년 8월 3일
댓글 수: 1
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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Interpolation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!