2

I want to use the \draw command in conjunction with relative coordinates in order to plot a slope triangle underneath a tangent to a graph, but the following syntax does not yield the desired result:

\draw [color=red] (-1,4) -- ++(0,-1) -- ++(0.5,0);

Instead, the result I obtain is the following:

enter image description here

Here is the complete code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage[ngerman]{babel} \usepackage{amsmath} \usepackage{tikz} \usetikzlibrary{positioning,intersections,shapes.geometric,automata,math}

\usepackage{pgfplots} \pgfplotsset{width=10cm,compat=newest} \usepgfplotslibrary{external} \tikzexternalize

\begin{document}

\begin{tikzpicture} [declare function={ f(\x) = \x^3 - 5\x; derf(\x) = 3\x^2 - 5; g(\x,\y) = derf(\y)*(\x-\y) + f(\y); }] \begin{axis}[ axis x line=center, axis y line=center, xtick=\empty, ytick=\empty, xlabel={$x$}, ylabel={$y$}, xlabel style={below right}, ylabel style={above left}, xmin=-3.5, xmax=3.5, ymin=-15, ymax=15 ]

\addplot [domain=-3:3,smooth,thick, color=black, name path=p1] {f(x)} node[above right, xshift=-9mm] {$y=f(x)$};

\tikzmath{\z1=-2.6;}
\addplot [only marks,mark=*] coordinates { (\z1,{f(\z1)}) };
\addplot [domain=-3.1:-2.1, color=black] { g(x,\z1) };
\draw [dashed] (\z1,0) -- (\z1,{f(\z1)});
\draw (\z1,2pt) -- (\z1,-2pt) node[above] {$x_1$};

\tikzmath{\z2=-1;}
\addplot [only marks,mark=*] coordinates { (\z2,{f(\z2)}) };
\addplot [domain=-2.3:0.3, color=black] { g(x,\z2) };
\draw [dashed] (\z2,0) -- (\z2,{f(\z2)});
\draw (\z2,2pt) -- (\z2,-2pt) node[below] {$x_2$};
\draw [color=red] (-1,4) -- ++(0,-1) -- ++(0.5,0);

\tikzmath{\z3=1.8;}
\addplot [only marks,mark=*] coordinates { (\z3,{f(\z3)}) };
\addplot [domain=0.8:2.8, color=black] { g(x,\z3) };
\draw [dashed] (\z3,0) -- (\z3,{f(\z3)});
\draw (\z3,2pt) -- (\z3,-2pt) node[above] {$x_3$};

\end{axis} \end{tikzpicture}

\end{document}

I am quite new to Tikz and Pgfplots and perhaps I just made a silly mistake. In any case, I would be really grateful if someone would help me fix this.

Apart from this I would also really appreciate any suggestions or advice to improve the code, simplify it and clean it up where possible. For example, I was wondering if the three blocks of code to plot the tangent lines could be replaced by just one block, using a common variable taking three different values instead of defining the three variables \z1, \z2, \z3separately.

Many thanks in advance!

Stefan Pinnow
  • 29,535
Apollo13
  • 341
  • Would you be interested by a simple pstricks solution? – Bernard Aug 19 '21 at 14:50
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – hpekristiansen Aug 19 '21 at 15:17
  • this answer and other answers in that post may be helpful https://tex.stackexchange.com/a/514500/140722 https://tex.stackexchange.com/questions/219747/normalized-tangent-vectors-along-a-curve – Black Mild Aug 19 '21 at 15:33
  • also this one https://tex.stackexchange.com/questions/461573/tangent-lines-diagram-along-smooth-curve/ – Black Mild Aug 19 '21 at 15:38
  • @Bernard Yes, absolutely. But being a beginner, I don't know what you mean by pstricks. Is it a package? – Apollo13 Aug 19 '21 at 20:05
  • @hpekristiansen Thank you, I have edited my post to include the preamble etc. – Apollo13 Aug 19 '21 at 20:05
  • Thank you @BlackMild – Apollo13 Aug 19 '21 at 20:06
  • @Apollo13: It is a package that is an interface between LaTeX and the Adobe Postscript language.. Contrary to TikZ, it uses a latex-like syntax. The basic usage consists of the following chain: latex >dvi > dvips > >pstopdf (that's because pdflatex doesn't have the tools the computations required by Postscript). Another possibility uses xelatex --shell-escape. – Bernard Aug 19 '21 at 20:12
  • B.t.w., what do you mean exactly with ‘a slope triangle’ here? – Bernard Aug 19 '21 at 20:22

3 Answers3

4

For fun, a code with pstricks, which defines a \psplotTangentcommand:

\documentclass[svgnames, border=6pt]{standalone}
\usepackage{pstricks-add}%

\begin{document}

\psset{linejoin=1, arrowinset=0.12, yunit=0.25cm, labelsep=3pt} \begin{pspicture}(-3.5,-15)(3.5,15) \psaxes[ticks=none, labels=none]{->}(0,0)(-3.5,-15)(3.5,15)[$x$,-110][$y$,-135]% \psplot[linewidth=1.2pt, linecolor=RoyalBlue, plotpoints=500, plotstyle=curve, algebraic]{-3.5}{3.5}{x^3-5x} \foreach \x in {-2.6,-1,1.8}{\psplotTangent[algebraic, linewidth=0.6pt]{\x}{2}{x^3-5x}} \foreach \x/\y in {-2.6/-4.58,-1/4,1.8/-3.17}{\psset{linestyle=dashed, dash=3pt 2pt, linewidth=0.4pt} \psline{-}(\x, \y)(\x, 0)} \uput[u](-2.6, 0){\footnotesize$-2.6$}\uput[d](-1, 0){\footnotesize$-1$}% \uput[u](1.8, 0){\footnotesize$1.8$} \pslinelinecolor=IndianRed(-1,2) \end{pspicture*}

\end{document}

enter image description here

Bernard
  • 271,350
3

Your code does not compile, so I offer only a one line solution instead of compilable code:

\draw [red] (-1,4) -- ++(axis direction cs:0,-1) -- ++(axis direction cs:0.5,0);  %ok now

https://tex.stackexchange.com/a/332603/8650

Edit:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}

\begin{tikzpicture}[ declare function={ f(\x) = \x^3 - 5\x; derf(\x) = 3\x^2 - 5; g(\x,\xzero) = derf(\xzero)*(\x-\xzero) + f(\xzero); x1=-2.6; x2=-1; x3=1.8; }]

\begin{axis}[ axis x line=center, axis y line=center, xtick=\empty, ytick=\empty, xlabel={$x$}, ylabel={$y$}, xlabel style={below right}, ylabel style={above left}, xmin=-3.5, xmax=3.5, ymin=-15, ymax=15, clip=false, xtick={x1, x2, x3}, xticklabel=\empty ]

\draw[dashed] (x1,0) node[above] {\footnotesize $x_1$} -- (x1,{f(x1)});
\addplot[domain=-3.1:-2.1] { g(x,x1) };
\draw plot[mark=*, mark size=1.5pt] coordinates { (x1,{f(x1)}) };

\draw[dashed] (x2,0) node[below] {\footnotesize $x_2$} -- (x2,{f(x2)});
\draw [red] (x2,{f(x2)}) -- ++(axis direction cs:0,-1) -- ++(axis direction cs:{-1/derf(x2)},0);
\addplot[domain=-1.8:-0.2] { g(x,x2) };
\draw plot[mark=*, mark size=1.5pt] coordinates { (x2,{f(x2)}) };

\draw[dashed] (x3,0) node[above] {\footnotesize $x_3$} -- (x3,{f(x3)});
\addplot[domain=1.2:2.3] { g(x,x3) };
\draw plot[mark=*, mark size=1.5pt] coordinates { (x3,{f(x3)}) };

\addplot [domain=-3:3,smooth,thick] {f(x)} node[above] {\footnotesize $y=f(x)$};

\end{axis} \end{tikzpicture} \end{document}

Graph

  • Thank you very much! Do you have any idea why my code did not work? I have edited my post to include the preamble. – Apollo13 Aug 19 '21 at 20:02
  • P.S. If you have any suggestions with regards to the second part of my question, I would be very grateful! – Apollo13 Aug 19 '21 at 20:03
  • Always keep your code minimal and relevant to your problem. You should especially not include any packages in the preamble that is not needed. I wrote my answer before your edit. Right now, your code is not compiling for me because of \tikzexternalize. Externalisation is not relevant for your problem! – hpekristiansen Aug 19 '21 at 21:04
  • I will look at your code a little later – hpekristiansen Aug 19 '21 at 21:04
  • Thank you! If you find the time, I would much appreciate that! P.S. If I delete \tikzexternalize from the preamble, I still get the same problem... By the way, I'm using overleaf. – Apollo13 Aug 20 '21 at 05:40
  • @Apollo13: The code can be made in many different ways - I add my preferred way. Instead of explaining it, is easier if you ask about things you might not understand. – hpekristiansen Aug 20 '21 at 13:58
  • Thank you very much! When I compile your code in Overleaf, the coordinate system, the curve and the tangent lines are drawn properly; all the rest, however, - that is, the marks, nodes and dashed lines – appear in the left bottom corner of the tikzpicture, all laid on top of each other... Do you think that's a problem of the overleaf web app? Which Tex-editor do you use? – Apollo13 Aug 21 '21 at 07:58
  • The LaTeX editor is the software in which you write your code(mine is TeXShop). It is not the engine that compiles the document(mine is macTeX). In overleaf, both is cloud based and not fully updated. ALWAYS look at the errors(red number top right), when you compile. Try to change compat=1.18 to compat=1.15. – hpekristiansen Aug 21 '21 at 09:33
1

Using the tzplot package:

enter image description here

\documentclass[tikz]{standalone}

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}[yscale=.2] %\tzhelplines(-4,-15)(4,15) \tzaxes(-4,-15)(4,15){$x$}[br]{$y$}[al] \def\Fx{(\x)^3-5\x} \tzfn[blue,thick]\Fx[-3:3]{$y=f(x)$}[a,black] % tangent \tzvXpointat{Fx}{-2.6}(x1) \tzvXpointat{Fx}{-1} (x2) \tzvXpointat*{Fx}{1.8} (x3) \tztangent[red] {Fx}(x1)[-3.1:-2.1] \tztangent[red]"tan"{Fx}(x2)[-2.1:0.2] \tztangent[red] {Fx}(x3)[1:2.6] % projection \tzprojx(x1){$x_1$}[a] \tzprojx(x2){$x_2$} \tzprojx(x3){$x_3$}[a] % slope triangle \tzvXpointat{tan}{-.5}(x2a) \tzlinkred|- \end{tikzpicture}

\end{document}

I. Cho
  • 2,985