2012-05-23 141 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 ^^ –