水平切线问题

水平切线问题

我试图弄清楚为什么右边的切线看起来稍微倾斜,而左边的另一条切线却在做它应该做的事情?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\newcommand*{\DeltaX}{0.01}
\newcommand*{\DrawTangentHor}[5][]{%
% #1 = draw options
% #2 = name of curve
% #3 = ymin
% #4 = ymax
% #5 = x value at which tangent is to be drawn

\path[name path=Vertical Line Left]  (#5-\DeltaX,#3) -- (#5-\DeltaX,#4);
\path[name path=Vertical Line Right] (#5+\DeltaX,#3) -- (#5+\DeltaX,#4);

\path [name intersections={of=Vertical Line Left and #2}];
\coordinate (X0) at (intersection-1);
\path [name intersections={of=Vertical Line Right and #2}];
\coordinate (X1) at (intersection-1);

\draw [shorten <= -1cm, shorten >= -1cm, #1] (X0) -- (X1) node[] {};
 }%

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1,cap=round]
\tikzset{axes/.style={}}
\draw[style=help lines,step=1cm, dotted] (-5,-5) grid (5,5);
 % The graphic
 \begin{scope}[style=axes]
 \draw[->] (-5.5,0) -- (5.5,0) node[below] {$x$};
 \draw[->] (0,-5.5)-- (0,5.5) node[left] {$y$};
 \foreach \x/\xtext in {-5/-5,-4/-4,-3/-3,-2/-2,-1/-1,1/1, 2/2, 3/3, 
 4/4,5/5}
  \draw[xshift=\x cm] (0pt,2pt) -- (0pt,-2pt) 
  node[below,fill=white,font=\normalsize]
   {$\xtext$};    
 \foreach \y/\ytext in {-5/-10,-4/-8,-3/-6,-2/-4,-1/-2,1/2, 2/4, 3/6, 
 4/8,5/10}
 \draw[yshift=\y cm] (2pt,0pt) -- (-2pt,0pt) 
 node[left,fill=white,font=\normalsize]
 {$\ytext$};
 \draw[name path=curve, domain=-2.6:3.35,smooth,variable=\x, black,<- 
  >,thick] plot ({\x},{-.5*\x*\x*\x+.5*\x*\x+.5*5*\x-.5*1});    
  %%%
 \DrawTangentHor[red,thick,shorten <= -1cm, shorten >= -1cm,-]{curve}{-5}{1}{-1}
 \DrawTangentHor[red,thick,shorten <= -1cm, shorten >= -1cm,-]{curve}{2.5}{3.5 }{1.666667}
 \draw[fill] (-1,-2) circle (2pt) node[below] {\scriptsize $(-1,-4)$};
 \draw[fill] (1.6667,2.740741) circle (2pt) node[above] {\scriptsize $(\frac{5}{3},5\frac{13}{27})$};
 \end{scope}
 \end{tikzpicture}
  \end{center}
\end{document}

输出:

在此处输入图片描述

答案1

发生这种情况是因为smooth路径扭曲了,而且在您的示例中,样本数量不是很大。当您增加样本时,就会发生这种情况:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{intersections}

\newcommand*{\DeltaX}{0.01}
\newcommand*{\DrawTangentHor}[5][]{%
% #1 = draw options
% #2 = name of curve
% #3 = ymin
% #4 = ymax
% #5 = x value at which tangent is to be drawn

\path[name path=Vertical Line Left]  (#5-\DeltaX,#3) -- (#5-\DeltaX,#4);
\path[name path=Vertical Line Right] (#5+\DeltaX,#3) -- (#5+\DeltaX,#4);

\path [name intersections={of=Vertical Line Left and #2}];
\coordinate (X0) at (intersection-1);
\path [name intersections={of=Vertical Line Right and #2}];
\coordinate (X1) at (intersection-1);

\draw [shorten <= -1cm, shorten >= -1cm, #1] (X0) -- (X1) node[] {};
 }%

\begin{document}
\foreach \X in {20,25,...,100}
{\begin{tikzpicture}[scale=1,cap=round]
\tikzset{axes/.style={}}
\draw[style=help lines,step=1cm, dotted] (-5,-5) grid (5,5);
 % The graphic
 \node[font=\sffamily] at (3,4) {\X\ samples};
 \begin{scope}[style=axes]
 \draw[->] (-5.5,0) -- (5.5,0) node[below] {$x$};
 \draw[->] (0,-5.5)-- (0,5.5) node[left] {$y$};
 \foreach \x/\xtext in {-5/-5,-4/-4,-3/-3,-2/-2,-1/-1,1/1, 2/2, 3/3, 
 4/4,5/5}
  \draw[xshift=\x cm] (0pt,2pt) -- (0pt,-2pt) 
  node[below,fill=white,font=\normalsize]
   {$\xtext$};    
 \foreach \y/\ytext in {-5/-10,-4/-8,-3/-6,-2/-4,-1/-2,1/2, 2/4, 3/6, 
 4/8,5/10}
 \draw[yshift=\y cm] (2pt,0pt) -- (-2pt,0pt) 
 node[left,fill=white,font=\normalsize]
 {$\ytext$};
 \draw[name path=curve,samples=\X, domain=-2.6:3.35,smooth,variable=\x, black,<- 
  >,thick] plot ({\x},{-.5*\x*\x*\x+.5*\x*\x+.5*5*\x-.5*1});    
  %%%
 \DrawTangentHor[red,thick,shorten <= -1cm, shorten >= -1cm,-]{curve}{-5}{1}{-1}
 \DrawTangentHor[red,thick,shorten <= -1cm, shorten >= -1cm,-]{curve}{2.5}{3.5 }{1.666667}
 \draw[fill] (-1,-2) circle (2pt) node[below] {\scriptsize $(-1,-4)$};
 \draw[fill] (1.6667,2.740741) circle (2pt) node[above] {\scriptsize $(\frac{5}{3},5\frac{13}{27})$};
 \end{scope}
\end{tikzpicture}}
\end{document}

在此处输入图片描述

根据经验,最好有奇数个样本,特别是如果曲线是对称的。如果您将样本与您的同步\DeltaX,则可以“优化”切线。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{intersections}

\newcommand*{\DeltaX}{0.01}
\newcommand*{\DrawTangentHor}[5][]{%
% #1 = draw options
% #2 = name of curve
% #3 = ymin
% #4 = ymax
% #5 = x value at which tangent is to be drawn

\path[name path=Vertical Line Left]  (#5-\DeltaX,#3) -- (#5-\DeltaX,#4);
\path[name path=Vertical Line Right] (#5+\DeltaX,#3) -- (#5+\DeltaX,#4);

\path [name intersections={of=Vertical Line Left and #2}];
\coordinate (X0) at (intersection-1);
\path [name intersections={of=Vertical Line Right and #2}];
\coordinate (X1) at (intersection-1);

\draw [shorten <= -1cm, shorten >= -1cm, #1] (X0) -- (X1) node[] {};
 }%

\begin{document}
\begin{tikzpicture}[scale=1,cap=round]
\tikzset{axes/.style={}}
\draw[style=help lines,step=1cm, dotted] (-5,-5) grid (5,5);
 % The graphic
 \begin{scope}[style=axes]
 \draw[->] (-5.5,0) -- (5.5,0) node[below] {$x$};
 \draw[->] (0,-5.5)-- (0,5.5) node[left] {$y$};
 \foreach \x/\xtext in {-5/-5,-4/-4,-3/-3,-2/-2,-1/-1,1/1, 2/2, 3/3, 
 4/4,5/5}
  \draw[xshift=\x cm] (0pt,2pt) -- (0pt,-2pt) 
  node[below,fill=white,font=\normalsize]
   {$\xtext$};    
 \foreach \y/\ytext in {-5/-10,-4/-8,-3/-6,-2/-4,-1/-2,1/2, 2/4, 3/6, 
 4/8,5/10}
 \draw[yshift=\y cm] (2pt,0pt) -- (-2pt,0pt) 
 node[left,fill=white,font=\normalsize]
 {$\ytext$};
 \draw[name path=curve,samples=119, domain=-2.6:3.35,smooth,variable=\x, black,<- 
  >,thick] plot ({\x},{-.5*\x*\x*\x+.5*\x*\x+.5*5*\x-.5*1});    
  %%%
 \DrawTangentHor[red,thick,shorten <= -1cm, shorten >= -1cm,-]{curve}{-5}{1}{-1}
 \DrawTangentHor[red,thick,shorten <= -1cm, shorten >= -1cm,-]{curve}{2.5}{3.5 }{1.666667}
 \draw[fill] (-1,-2) circle (2pt) node[below] {\scriptsize $(-1,-4)$};
 \draw[fill] (1.6667,2.740741) circle (2pt) node[above] {\scriptsize $(\frac{5}{3},5\frac{13}{27})$};
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容