曲线有多条切线

曲线有多条切线

我用用于绘制任意图的切线。例如:

\documentclass[border=5mm]{standalone}  
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{intersections}

\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}
\makeatother

\begin{document}

    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    axis x line=center,
    axis y line=center,
    xtick={-5,-4,...,5},
    ytick={-5,-4,...,5},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    xmin=-5.5,
    xmax=5.5,
    ymin=-5.5,
    ymax=5.5,
    tangent/.style={
        add node at x={2}{
            [
            sloped, 
            append after command={(\tikzlastnode.west) edge [thick,black] (\tikzlastnode.east)},
            minimum width=0.2\textwidth
            ]
        }      
    }]
        \addplot[color=black,smooth, tangent] coordinates {
        (-5,-5)
        (-3,-4)
        (-2,-1)
        (0,0)
        (1,2)
        (2,3)
        (3,3)
        (4,1)
    };
    \end{axis}
    \end{tikzpicture}

\end{document}

在此处输入图片描述

问题是我想要更多切线,不只一个。例如,在这一点上(-3,f(-3))。 是否可以?

我尝试了一些解决方案,但只得到了错误。也许我不理解 Jake 的代码...

先谢谢了!!!

答案1

像这样?我稍微修改了一下tangent/.style,使它成为位置的参数,然后您可以使用列表键处理程序来执行

 tangent/.list={2,3}

梅威瑟:

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}
\makeatother

\begin{document}

    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    axis x line=center,
    axis y line=center,
    xtick={-5,-4,...,5},
    ytick={-5,-4,...,5},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    xmin=-5.5,
    xmax=5.5,
    ymin=-5.5,
    ymax=5.5,
    tangent/.style={
        add node at x={#1}{
            [
            sloped, 
            append after command={(\tikzlastnode.west) edge [thick,black] (\tikzlastnode.east)},
            minimum width=0.2\textwidth
            ]
        }      
    }]
        \addplot[color=black,smooth, tangent/.list={2,3}] coordinates {
        (-5,-5)
        (-3,-4)
        (-2,-1)
        (0,0)
        (1,2)
        (2,3)
        (3,3)
        (4,1)
    };
    \end{axis}
    \end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容