2016-02-12 85 views
0

在Matlab中,我有一個散點圖,其中x軸和y軸都是對數刻度。我如何在日誌範圍內添加一條最適合的線? 謝謝!在matlab中通過散射點的對數尺度的polyfit/polyval

x = [0.0090 0.0000 0.0001 0.0000 0.0001 0.0000 0.0097 0.0016 0.0006 0.0000 0.0016 0.0013 0.0023]; 
y = [0.0085 0.0001 0.0013 0.0006 0.0005 0.0006 0.0018 0.0076 0.0015 0.0001 0.0039 0.0015 0.0024]; 
scatter(x,y) 
set(gca,'YScale','log'); 
set(gca,'XScale','log'); 
hold on 
p = polyfit(log(x),log(y),1); 
f = polyval(p,x); 
plot(x,f,'Color',[0.7500 0.7500 0.7500],'linewidth',2) 
+0

你如何計劃'polyfit'無窮?因爲這就是你要從log(0)得到的結果。 – 2016-02-12 01:11:05

回答

1

當最適合的搜索,你需要使用原始數據xy,而不是他們的日誌。日誌量表僅用於表示結果。

在使用polyval之前,您需要對x進行排序。使用普通軸時無關緊要,但由於順序錯誤,可能會出現對數軸奇怪的現象。

這裏的情節:

enter image description here

代碼:

x = [0.0090 0.0000 0.0001 0.0000 0.0001 0.0000 0.0097 0.0016 0.0006 0.0000 0.0016 0.0013 0.0023]; 
y = [0.0085 0.0001 0.0013 0.0006 0.0005 0.0006 0.0018 0.0076 0.0015 0.0001 0.0039 0.0015 0.0024]; 
scatter(x,y); 
set(gca,'YScale','log'); 
set(gca,'XScale','log'); 
hold on; 
x_sort = sort(x); 
p = polyfit(x,y,1); 
f = polyval(p,x_sort); 
plot(x_sort,f,'Color',[0.7500 0.7500 0.7500],'linewidth',2); 

它是你想要的嗎?