2012-05-23 140 views

回答

2

如果您沒有繪製點的顯式函數,則可以使用finite differences來估計導數。以下是適當的點不是對數據的時間跨度的邊界:

plot(x,y); 
hold all; 

% first sort the points, so x is monotonically rising 
[x, sortidx] = sort(x); 
y = y(sortidx); 

% this is the x point for which you want to compute the slope 
xslope = (x(1)+x(end))/2; 

idx_a = find(x<xslope,1,'last'); 
idx_b = find(x>xslope,1,'first'); 
% or even simpler: 
idx_b = idx_a+1; 
% this assumes min(x)<xslope<max(x) 

xa = x(idx_a); 
xb = x(idx_b); 
slope = (y(idx_b) - y(idx_a))/(xb - xa); 

現在繪製的斜率,這取決於你想要什麼:僅短短一行:

yslope = interp1(x,y,xslope); 
ya_sloped = yslope + (xa-xslope)*slope; 
yb_sloped = yslope + (xb-xslope)*slope; 
line([xa;xb],[ya_sloped;yb_sloped]); 

或更長的線

yslope = interp1(x,y,xslope); 
xa = xa + 4*(xa-xslope); 
xb = xb + 4*(xb-xslope); 
ya_sloped = yslope + (xa-xslope)*slope; 
yb_sloped = yslope + (xb-xslope)*slope; 
line([xa;xb],[ya_sloped;yb_sloped]); 

我敢肯定有是在此代碼沒有錯誤,但我會測試它時,我有MATLAB左右;)

1

你必須找出你有興趣使用(y2-y1)/(x2-x1)的任何點的斜率,然後用plot()畫出一條具有該斜率的直線。要繪製直線,需要y軸截距,並且由於您知道該線上至少有一個點的座標(這是要繪製切線的點),因此可以在方程y = mx中求解b + B.

+0

我的計數器把這個答案重新寫回0.這不是最好的答案,它沒有洞察到matlab代碼,但它的概念上是合理的,所以我認爲它不值得-1 –

+0

我同意這種方法,但我會添加根據圖形的水平分辨率(您必須繪製多少個點),您可能需要先考慮插值以獲得更準確的結果。在matlab中看到interp(),這很簡單。 – Dan

+0

我與Xurtio ^^ –