绘制平滑曲线的切线

绘制平滑曲线的切线

我想复制此图在此处输入图片描述

它说明了函数的导数。我无法为图像所示的两条限制线写标签,也无法添加更多点和线。这是我尝试的:

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=newest}

\begin{document}
\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[
      axis x line=center, 
      axis y line=middle, 
      xlabel=$x$,
      ylabel=$y$,
      restrict y to domain=0:600,      domain=0:11
    ]
    \addplot[blue,thick] {x+5*x^2};
    \addplot[color=blue,only marks,mark=*] coordinates{(8,328)};
    \foreach \ta in {4,4.5,5,5.5,6,6.5,7} {
      \pgfmathsetmacro{\Sa}{\ta+5.0*\ta^2}% <- changed
        \only<+-+>{
          \addplot[color=blue,only marks,mark=*] coordinates{(\ta,\Sa)};
          \pgfmathsetmacro{\m}{(328.0-\Sa)/(8.0-\ta)};
          \pgfmathsetmacro{\b}{328-\m*8.0};
          \addplot[black,thick] {\m*x+\b};
          \addplot[red,thick] {\m};
        };
    }
  \end{axis}
  \end{tikzpicture}
\end{frame}
\end{document}

答案1

这是添加标签和切线的一种方法(非常感谢@Teepeemm)。

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=newest}

\begin{document}
\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[
      axis x line=center, 
      axis y line=middle, 
      xlabel=$x$,
      ylabel=$y$,
      restrict y to domain=0:600,      domain=0:11
    ]
    \addplot[blue,thick] {x+5*x^2};
    \addplot[color=blue,only marks,mark=*,
    nodes near coords,point meta=explicit symbolic] coordinates{(8,328) [Q]};
    \foreach \ta in {4,4.5,5,5.5,6,6.5,7} {
      \pgfmathsetmacro{\Sa}{\ta+5.0*\ta^2}% <- changed
        \only<+-+>{
          \addplot[color=blue,only marks,mark=*,
          nodes near coords,point meta=explicit symbolic] 
          coordinates{(\ta,\Sa) [P]};
          \addplot[color=purple,domain=-2:4,samples=2,thick]  
          (\ta+x,{\Sa+x+10*\ta*x}) 
          coordinate[pos=0.9](aux3) coordinate[pos=1](aux4);
          \pgfmathsetmacro{\m}{(328.0-\Sa)/(8.0-\ta)};
          \pgfmathsetmacro{\b}{328-\m*8.0};
          \addplot[black,thick] {\m*x+\b} coordinate[pos=0.9](aux1) coordinate[pos=1](aux2);
          %\addplot[red,thick] {\m};
        };
    }
  \end{axis}
  \path (aux1) -- (aux2) node[pos=1,sloped,anchor=north east]{secante};
  \path (aux3) -- (aux4) node[pos=1,sloped,anchor=north east]{tangente};
  \end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

背景颜色是由转换为动画 gif 而产生的,在 pdf 中没有出现。如果我遗漏了什么,请告诉我。

附录:尝试让@Teepeemm 和@Sigur 更开心。它也可以说更优雅,因为它使用函数(via declare fucntion)而不是硬编码值。

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=newest}
\begin{document}
\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[declare function={f(\x)=\x+5*\x*\x;},
      axis x line=center, 
      axis y line=middle, 
      xlabel=$x$,
      ylabel=$y$,
      restrict y to domain=0:600,      domain=0:11
    ]
    \addplot[blue,thick] {f(x)} coordinate[pos=0.95] (aux0);
    \addplot[color=blue,only marks,mark=*,samples at=4] {f(x)} node[above]{$P$};
    \foreach \ta in {9.5,9,...,4.5} {
     \only<+-+>{
      \addplot[color=blue,only marks,mark=*,samples at=\ta] {f(x)} node[above]{$Q$};
      \pgfmathsetmacro{\m}{(f(\ta)-f(4))/(\ta-4)};
      \pgfmathsetmacro{\b}{f(4)-\m*4};
      \addplot[black,thick] {\m*x+\b} coordinate[pos=0.9](aux1) coordinate[pos=1](aux2);
      \addplot[red,thick] {\m};
        \addplot[color=purple,domain={-0.5*\ta+1}:0.5,samples=2,thick]
         (\ta+x,{f(\ta)+x*(f(\ta+0.1)-f(\ta-0.1))/0.2}) coordinate[pos=0.9](aux3)
          coordinate[pos=1](aux4);        
    }
     }
  \end{axis}
   \path (aux0) node[blue,right]{$y=f(x)$};
   \path (aux1) -- (aux2) node[pos=1,sloped,anchor=north east]{secante};
   \path (aux3) -- (aux4) node[pos=1,sloped,anchor=north east]{tangente};
  \end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

这是一个非动画版本。

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=newest}
\usepackage{contour}
\contourlength{0.3pt}
\begin{document}
\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[declare function={f(\x)=\x+5*\x*\x;},
      axis x line=center, 
      axis y line=middle, 
      xlabel=$x$,
      ylabel=$y$,
      restrict y to domain=0:600,      domain=0:11
    ]
    \addplot[blue,thick] {f(x)} coordinate[pos=0.95] (aux0);
    % add P
    \addplot[color=blue,only marks,mark=*,samples at=4] {f(x)} 
    coordinate(P)   node[above]{$P$};
    % add tangent at P
    \addplot[color=purple,domain={-1}:0.5,samples=2,thick]  
          (4+x,{f(4)+x+10*4*x}) 
          coordinate[pos=0.9](aux3) coordinate[pos=1](aux4);
    % add various P values    
    \foreach \ta [count=\Y] in  {4.5,5.75,...,9.5} {
      \edef\temp{\noexpand\addplot[color=blue,only marks,mark=*,samples at=\ta] {f(x)} 
      coordinate(Q-\Y) ;}
      \temp
     }
  \end{axis}
  \path (aux0) node[blue,right]{$y=f(x)$};
  \foreach \Y in {5,4,...,1}
  {\draw[thick,shorten >=-5mm] (P) -- (Q-\Y) 
  node[above,blue]{\contour{white}{$Q$}}
  \ifnum\Y>1
  node[pos=1,sloped,anchor=north]{secante}
  \fi;}
  \path (aux3) -- (aux4) node[pos=1,sloped,anchor=north east]{tangente};
  \end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

如果希望secante标签只出现一次,请使用\ifnum\Y=5而不是\ifnum\Y>1

编辑:删除了未使用的功能fprime。非常感谢 Julien-Elie Taieb 指出这一点!

相关内容