答案1
钛钾Z 会自动计算切线,例如,当使用装饰时,您会自动处于“切线空间”。因此,您需要做的就是在“切线空间”中添加由水平线组成的标记。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{tangent/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {
\draw[red] (0pt,0pt) coordinate (Tang) -- (200pt,0pt);)}}}}}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=3*sin(10*\x+20);}]
\draw[latex-latex] (0,5) -- (0,0) -- (8,0);
\begin{scope}
\clip (0,0) rectangle (7,5);
\draw[tangent=0.2] plot[samples=100,domain=0:7] ({\x},{f(\x)});
\end{scope}
\draw[dashed] (Tang) --(0,-0.1 -| Tang) node[below]{$x_0$};
\end{tikzpicture}
\end{document}
啊,我可能应该提到,你当然可以使用它在固定x
值处绘制切线。假设你想在 处绘制它x=1.3
。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{tangent/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {
\draw[red] (0pt,0pt) coordinate (Tang) -- (200pt,0pt);)}}}}}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=3*sin(10*\x+20);}]
\draw[latex-latex] (0,5) -- (0,0) -- (8,0);
\begin{scope}
\clip (0,0) rectangle (7,5);
\draw plot[samples=100,domain=0:7] ({\x},{f(\x)});
\path[tangent=0] plot[samples=100,domain=1.3:7] ({\x},{f(\x)});
\end{scope}
\draw[dashed] (Tang) --(0,-0.1 -| Tang) node[below]{$x_0=1.3$cm};
\end{tikzpicture}
\end{document}
(是的,可以避免绘制两次图。但所有可能性都会使事情变得不必要地复杂。并且可以避免红线的硬编码长度并避免\clip
,但同样要付出的代价是复杂性的增加。)
而如今强制性的动画是不能缺少的。;-)
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{tangent/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {
\draw[red] (0pt,0pt) coordinate (Tang) -- (200pt,0pt);)}}}}}
\begin{document}
\foreach \X in {0.5,0.6,...,6}
{\begin{tikzpicture}[declare function={f(\x)=3*sin(10*\x+20);}]
\draw[latex-latex] (0,5) -- (0,0) -- (8,0);
\begin{scope}
\clip (0,0) rectangle (7,5);
\draw plot[samples=100,domain=0:7] ({\x},{f(\x)});
\path[tangent=0] plot[samples=100,domain={\X}:7] ({\x},{f(\x)});
\end{scope}
\pgfmathsetmacro{\Y}{int(10*\X)}
\draw[dashed] (Tang) --(0,-0.1 -| Tang) node[below]{$x_0=\Y$mm};
\end{tikzpicture}}
\end{document}