1

Let be the function

f(x) = -(1/162) x^5 + 17/162 x^4 - 43/162*x^3 - 179/162*x^2 + 212/81* x + 134/81

The solutions of the equation f'(x) = 0 are

x=-2\lor x=1\lor x=4\lor x=\frac{53}{5}

I tried to draw the tangents at the points x = -2, x = 1, x=4. I got the incorrect result.

My code

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
    \makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}
\makeatother

\begin{document}
    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    %axis y line =none,
    %axis x line =none,
    axis x line=center,
    axis y line=center,
    xtick=\empty,
    ytick=\empty,
    %xtick={-5,-4,...,5},
    %ytick={-5,-4,...,5},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below},
    ylabel style={left},
    xmin=-5.5,
    xmax=5.5,
    ymin=-5.5,
    ymax=5.5,
    tangent/.style={
        add node at x={#1}{
            [
            sloped, 
            append after command={(\tikzlastnode.west) edge [thick,black] (\tikzlastnode.east)},
            minimum width=0.2\textwidth
            ]
        }      
    }]
       \addplot[color=blue,smooth,samples=500, thick,tangent/.list={-2,1,4},domain=-5:5] {-(1/162)*pow(\x,5)+17/162*pow(\x,4)-43/162*pow(\x,3)-179/162*pow(\x,2)+212/81*\x+134/81};
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

3
  • Please add \usepgfplotslibrary{fillbetween} or \usetikzlibrary{intersections} to the preamble such that the code runs through.
    – user121799
    Jun 16 '19 at 14:26
  • I added, and I still got incorrect result. Jun 16 '19 at 14:29
  • Yes, I know. I just wanted to achieve that the code runs through.
    – user121799
    Jun 16 '19 at 14:34
2

What you are seeing here are the effects of numerical accuracy. The way the tangents are drawn is that the graph is "checked" at the actual x value and, in the original code, 1pt right of it. If you replace 1pt by a smaller distance, and make the prescription more symmetric, you arrive at

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
    \makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [xshift=-0.1pt,name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=0.1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}
\makeatother

\begin{document}
    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    %axis y line =none,
    %axis x line =none,
    axis x line=center,
    axis y line=center,
    xtick=\empty,
    ytick=\empty,
    %xtick={-5,-4,...,5},
    %ytick={-5,-4,...,5},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below},
    ylabel style={left},
    xmin=-5.5,
    xmax=5.5,
    ymin=-5.5,
    ymax=5.5,
    tangent/.style={
        add node at x={#1}{
            [
            sloped, 
            append after command={(\tikzlastnode.west) edge [thick,black] (\tikzlastnode.east)},
            minimum width=0.2\textwidth
            ]
        }      
    }]
       \addplot[color=blue,smooth,samples=501, thick,tangent/.list={-2,1,4},domain=-5:5] {-(1/162)*pow(\x,5)+17/162*pow(\x,4)-43/162*pow(\x,3)-179/162*pow(\x,2)+212/81*\x+134/81};
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

This seems to do the job here but clearly I do not claim that 0.1pt is the "optimal" distance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.