在曲线上绘制切线

在曲线上绘制切线

我想在(x0)上画一条切线,它是垂直线和红色曲线的交点,这是我的代码

\documentclass[tikz,preview,border=1mm]{standalone}
\usetikzlibrary{calc,intersections,arrows}
\def\w{0.7}
\begin{document}
\begin{tikzpicture}[>=latex]
\draw[->,line width=0.5pt](-0.3,0) -- (5.2,0);
\draw[->,line width=0.5pt](0,-0.3) -- (0,3.5);

\draw[name path=curve,red] plot[samples=100,smooth,domain=0:3,variable=\t] 
({1+cos(deg(\w* \t)) * 5},{5+- sin(deg(\w*\t)) * \w* 5});

\draw[name path=vline](3,0.0)--+(90:3);

\path[name intersections={of=curve and vline, by=x0}];      
\fill (x0) circle(1pt);
\end{tikzpicture}
\end{document}

答案1

一个或多或少直接的想法是计算两个非常接近图上想要绘制切线的相关坐标的坐标,然后通过这两个坐标画一条线。

由于示例中的图未使用线性函数,因此我添加了反向函数以便更轻松地获取正确的值。

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{intersections, calc}

\begin{document}
\begin{tikzpicture}[
    >=latex,
    declare function={
        w = 0.7;
        curvex(\t) = 1+cos(deg(w*\t))*5;
        curvey(\t) = 5-sin(deg(w*\t))*w*5;
        reversex(\x) = rad(acos((\x-1)/5))/w;
    },
]

\draw[->, line width=0.5pt] (-0.3,0) -- (5.2,0);
\draw[->, line width=0.5pt] (0,-0.3) -- (0,3.5);

\draw[name path=curve, red] 
    plot[samples=100, smooth, domain=0:3, variable=\t] 
    ({curvex(\t)},{curvey(\t)});

\draw[name path=vline] (3,0) -- +(90:3);

\path[name intersections={of=curve and vline, by=x0}];      
\fill (x0) circle[radius=1pt];

\pgfmathsetmacro{\i}{reversex(3)}
\coordinate (x1) at ({curvex(\i-0.01)},{curvey(\i-0.01)});
\coordinate (x2) at ({curvex(\i+0.01)},{curvey(\i+0.01)});

\draw[green] ($(x1)!-2cm!(x2)$) -- ($(x1)!2cm!(x2)$);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

如果你能摆脱“平滑”部分(这会产生数值不稳定),那么(来自https://tex.stackexchange.com/a/477208/38080) 的工作原理:

\documentclass[tikz,preview,border=1mm]{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc,intersections,arrows}
\usetikzlibrary{decorations.markings}
\tikzset{tangent at/.style={% cf. https://tex.stackexchange.com/questions/25928/how-to-draw-tangent-line-of-an-arbitrary-point-on-a-path-in-tikz/25940#25940
        decoration={ markings,
            mark =at position #1 with {\draw[purple,-latex](0,0) -- (-1,0);},
        }, decorate
    }
}
\def\w{0.7}

\begin{document}
\begin{tikzpicture}[>=latex]
\draw[->,line width=0.5pt](-0.3,0) -- (5.2,0);
\draw[->,line width=0.5pt](0,-0.3) -- (0,3.5);

\draw[name path=curve,red] plot[samples=100,domain=0:3,variable=\t]
({1+cos(deg(\w* \t)) * 5},{5+- sin(deg(\w*\t)) * \w* 5});
\draw[name path=vline](3,0.0)--+(90:3);
\path[name intersections={of=curve and vline, by=x0}];
\fill (x0) circle(1pt);

\path [draw,blue,
    name path=section,
    intersection segments={
        of=curve and vline,
        sequence = {L2},
    },
    postaction={tangent at=0}
    ];

\end{tikzpicture}
\end{document}

在此处输入图片描述

您可以删除draw, blue仅用于直观查看sequence键的效果的...https://tikz.dev/pgfplots/libs-fillbetween#sec-46.6

相关内容