有人可以帮我找到曲线的切线。 我有两个数据集(时间,临时)。现在我首先使用对数函数拟合这些数据,现在我想在鼠标点击上画一条切线。我上传了一个有用的代码,但我不知道如何修改它。谢谢你的帮助。
function test
hh = figure(1); clf, hold on
grid on
x = 0:0.01:2*pi;
f = @(x) sin(x);
fprime = @(x) cos(x);
plot(x, f(x), 'r')
axis tight
D = [];
L = [];
set(hh, ...
'WindowButtonMotionFcn', @mouseMove,...
'WindowButtonDownFcn', @mouseClick);
function mouseMove(varargin)
coords = get(gca, 'currentpoint');
xC = coords(1);
if ishandle(D)
delete(D); end
D = plot(xC, f(xC), 'ko');
end
function mouseClick(obj, varargin)
switch get(obj, 'selectiontype')
% actions for left mouse button
case 'normal'
coords = get(gca, 'currentpoint');
xC = coords(1);
yC = f(xC);
a = fprime(xC);
b = yC-a*xC;
if ishandle(L)
delete(L); end
L = line([0; 2*pi], [b; a*2*pi+b]);
case 'alt'
% actions for right mouse button
case 'extend'
% actions for middle mouse button
case 'open'
% actions for double click
otherwise
% actions for some other X-mouse-whatever button
end
end
end