1

特定のポイントで曲線に接線を描く必要があります (ポイントがユーザーによって選択されたとします)。ユーザーが手動で 2 つの点を選択し、それらの間に線を引くことができるコードを作成しました。しかし、私はプロセスを自動化したいと考えています。誰かがそうするためにアルゴリズム/すでに実装されているmatlabコードを提案できますか?

4

1 に答える 1

6

以下の機能をお試しください。もちろん、あなたのケースに適用するには多くの微調整が必​​要ですが、これは大まかにあなたが望むものだと思います.

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
于 2012-11-26T12:00:21.380 に答える