12

I'd like to reproduce this graphic enter image description here

which illustrates the derivate of a function. I'm not able to write the labels for the two limiting lines as the image illustrates nor to add more points and lines. This is what I tried:

\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}
Stefan Pinnow
  • 29,535

1 Answers1

9

Here is a way to add the labels and a tangent (big thanks to @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}

enter image description here

The coloring of the background is from the conversion to an animated gif and does not occur in the pdf. Please let me know if I missed something.

ADDENDUM: An attempt to make @Teepeemm and @Sigur happier. It also is arguably more elegant since it uses a function (via declare fucntion) instead of hard-coded values.

\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}

enter image description here

And here is a non-animated version.

\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}

enter image description here

If you want the secante label to appear only once, use \ifnum\Y=5 instead of \ifnum\Y>1.

EDIT: Removed the function fprime which wasn't used. Big thanks to Julien-Elie Taieb for pointing that out!

  • 1
    @Teepeemm Thanks! The animation is actually the OP's code with some labels added. That is, I did not change the direction of who is moving towards whom. But I will add a tangent. Thanks! –  Jan 03 '20 at 15:07
  • Why is the horizontal red line moving? What does it represent? – Sigur Jan 03 '20 at 15:23
  • @Sigur I do not know, it is from the OP's code. –  Jan 03 '20 at 15:29
  • According to code, it is the slope of secant, I think. – Sigur Jan 03 '20 at 15:32
  • @Sigur I think OP's image has Q moving toward P, while OP's code has the horizontal red line and P moving toward Q. – Teepeemm Jan 03 '20 at 15:35
  • @Sigur Thanks! It is not clear to me, though, if this line is always useful, imagine for instance the axes carrying dimensions. –  Jan 03 '20 at 15:36
  • @Teepeemm Yes, since h should go to zero, Q should move. – Sigur Jan 03 '20 at 15:39
  • 1
    @Teepeemm I added a second version. –  Jan 03 '20 at 15:57
  • it's really a good job, thank you. I'd like the points that appear in the animation to appear also in the pdf (like in the image uploaded there are 4 secants and 4 point for each one) and if it is possible to put $x_0$ and $x_0+h$ on the x axis in correspondence of P and Q – Michaelangelo Meucci Jan 03 '20 at 16:29
  • @MichaelangeloMeucci Can you try to be more precise? "stationary" seems at odds with being animated. What precisely should move how? –  Jan 03 '20 at 16:30
  • 1
    @Schrödinger'scat what a great pedagogic tool. I was learning on your code (animated with definie function) but I did not see where you reused \fprime ? – JeT Apr 14 '20 at 23:00
  • @Julien-ElieTaieb Yes, I think you are right: it is not used. Good catch! –  Apr 14 '20 at 23:05
  • I am a faithful follower! – JeT Apr 14 '20 at 23:06
  • shoudn't it be replaced by \addplot[color=purple,domain={-0.5*\ta+1}:0.5,samples=2,thick] (\ta+x,{f(\ta)+fprime(\ta)*x}) coordinate[pos=0.9](aux3) coordinate[pos=1](aux4); – JeT Apr 14 '20 at 23:15
  • 1
    @Julien-ElieTaieb Yes, that would work. I now compute the derivative numerically. I have checked that at least in this example the precision is really good. So the user needs only to specify the function. –  Apr 14 '20 at 23:28
  • Merci the cat! I tried the code with the explicit derivative of f but I get an error on the path aux3 aux4. – JeT Apr 14 '20 at 23:34
  • 1
    @Julien-ElieTaieb On my machine the code works. –  Apr 14 '20 at 23:39