How do create a loglog line of best fit?

27 ビュー (過去 30 日間)
Nicholas Green
Nicholas Green 2015 年 9 月 23 日
コメント済み: Star Strider 2022 年 11 月 16 日
So far I've plotted my data and found that a loglog plot gives the most linear result. The line of best fit, however, isn't linear. How do i fix it? It also displays "negative numbers ignored" which might tie into it.
%%HK
close all % closes all windows
clear all % clae all variables
clc % clear command windows
beep off % turn beep noise off
format compact % tight spacing
%%Data
temp = [100,200,400,600,800,1000,1200,1400,1600]
density = [3.5,1.7,0.85,0.6,0.45,0.35,0.3,0.25,0.2]
%%Plot
loglog(temp, density, '*')
xlabel('Temperature T,K')
ylabel('Density D, kg/m^3')
title('Temperature vs. Density')
grid on
%%Equation constants
Const = polyfit(temp,density, 1)
m = Const(1)
k = Const(2)
%%Bestfit Line
hold on
YBL = m*temp+k
loglog(temp,YBL)

採用された回答

Star Strider
Star Strider 2015 年 9 月 23 日
Try this:
Const = polyfit(log(temp),log(density), 1)
m = Const(1)
k = Const(2)
YBL = x.^m.*exp(k)
  19 件のコメント
Les Beckham
Les Beckham 2022 年 11 月 16 日
Excellent example @Star Strider. Thumbs up.
I guess I should have emphasized that point more than with the cursory sentences "Be careful of using points that aren't spanned by the range of the original fit. Extrapolation can be dangerous (give you misleading results)."
Star Strider
Star Strider 2022 年 11 月 16 日
@Les Beckham — Thank you!
I thought the concept needed an illustration, so I provided one.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 9 月 23 日
logx = log(temp);
logy = log(density);
Const = polyfit(logx, logy, 1)
hold on
plot(temp, exp(polyval(Const, logx)));

カテゴリ

Help Center および File ExchangeNonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by