我已经用matlab绘制了一个图表:
plot(x,y)我的图形有不同的斜率,如何在每个斜率上绘制切线并计算斜率的系数?
发布于 2012-05-24 05:46:48
如果对绘制点没有显式函数,可以使用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]);我非常确定这段代码中没有but,但我会在有matlab的时候测试它;)
发布于 2012-05-24 05:37:59
您必须计算出您感兴趣的任何点的斜率(y2-y1)/(x2-x1),然后使用plot()绘制一条具有该斜率的直线。要绘制这条直线,您需要y截距,并且由于您知道该直线上至少一个点的坐标(这是您想要绘制切线的点),因此您可以在等式y=mx+b中求解b。
https://stackoverflow.com/questions/10728044
复制相似问题