3

I want to draw the axis of symmetry of a quadratic function, using the top. The problem is that positioning the axe is not so good. Changing the xmin=-6 into xmin=-2 makes the red line on the wrong place, also the top is not on the right place. Therefore, I want to define the redline with the x-coordinate of the max or min of the function. So that changing f(x), adapts the drawing.

The best I could come with is this:

\documentclass[symmetric,justified,a4paper,sfsidenotes]{tufte-book} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{pgf,tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usepackage{pgfplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.5]
\begin{axis}%
    [
            anchor=center,
        grid=major,
        x=1cm, y=1cm,
        minor x tick num=1, 
        xmin=-6, xmax=6,
        xlabel={\scriptsize $x$},
        axis x line=middle,
        minor y tick num=1,  
        ymin=-6, ymax=6,
        ylabel={\scriptsize $f(x)$},
        tick label style={font=\tiny},
        axis y line=middle,
        no markers,
        samples=100,
        domain=-6:6,
        restrict y to domain=-20:20,
    ]
    \addplot[black, thick] (x,{2(x-2)^2-3});
 \end{axis}
 \draw[color=red, thick] (2,-6) -- (2,6) node[right] {\scriptsize $x=2$};
 \coordinate (T) at (2,-3) ;
\tkzDrawPoints(T);
\node[right] at (T) {$T(p,q)$};
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

2 Answers2

6

Here's one way to do it automatically finding the minimum (works also for maximum):

enter image description here

The code:

\documentclass[symmetric,justified,a4paper,sfsidenotes]{tufte-book} % use larger type; default would be 10pt
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{pgf,tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usetkzobj{all} %om hoeken te markeren?
\usepackage{pgfplots}

\makeatletter
\pgfplotsset{
    compat=1.12,
    /tikz/max node/.style={
        anchor=south,
    },
    /tikz/min node/.style={
        anchor=north,
        name=minimum
    },
    mark min/.style={
        point meta rel=per plot,
        visualization depends on={x \as \xvalue},
        scatter/@pre marker code/.code={%
            \ifx\pgfplotspointmeta\pgfplots@metamin
                \def\markopts{}%
                \coordinate (minimum);
                \xdef\XMinimum{\xvalue},%
                \xdef\YMinimum{\pgfplotspointmeta},%
            \else
                \def\markopts{mark=none}
            \fi
            \expandafter\scope\expandafter[\markopts,every node near coord/.style=green]
        },%
        scatter/@post marker code/.code={%
            \endscope
        },
        scatter,
    },
    mark max/.style={
        point meta rel=per plot,
        visualization depends on={x \as \xvalue},
        scatter/@pre marker code/.code={%
        \ifx\pgfplotspointmeta\pgfplots@metamax
            \def\markopts{}%
               \coordinate (maximum);
                \xdef\XMaximum{\xvalue},%
                \xdef\YMaximum{\pgfplotspointmeta},%
        \else
            \def\markopts{mark=none}
        \fi
            \expandafter\scope\expandafter[\markopts]
        },%
        scatter/@post marker code/.code={%
            \endscope
        },
        scatter
    }
}
\makeatother

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}%
    [
        anchor=center,
        grid=major,
        minor x tick num=1, 
        xmin=-6, xmax=6,
        xlabel={\scriptsize $x$},
        axis x line=middle,
        minor y tick num=1,  
        ymin=-6, ymax=6,
        ylabel={\scriptsize $f(x)$},
        tick label style={font=\tiny},
        axis y line=middle,
        no markers,
        samples=100,
        domain=-6:6,
        after end axis/.code={
          \draw[red,thick] (minimum|-{axis cs:0,-6}) --(minimum|-{axis cs:0,6});
          \tkzDrawPoints(minimum);
          \node[below right] at (minimum) {$T(p,q)$};
          \node[below right,red] at (minimum|-{axis cs:0,6}) {\scriptsize $x=\pgfmathprintnumber[fixed]{\XMinimum}$};
      }
    ]
\addplot[black, thick,mark min] {2*(x-2)^2-3};
\end{axis}
\coordinate (T) at (2,-3) ;
\end{tikzpicture}
\end{center}

\begin{center}
\begin{tikzpicture}
\begin{axis}%
    [
        anchor=center,
        grid=major,
        minor x tick num=1, 
        xmin=-4, xmax=4,
        ymax=70,ymin=-30,
        xlabel={\scriptsize $x$},
        axis x line=middle,
        minor y tick num=1,
        ylabel={\scriptsize $f(x)$},
        tick label style={font=\tiny},
        axis y line=middle,
        no markers,
        samples=100,
        domain=-5:5,
        after end axis/.code={
          \draw[red,thick] (minimum) -- (minimum|-{axis cs:0,70});
          \draw[red,thick] (minimum-|{axis cs:-4,0}) -- (minimum-|{axis cs:4,0});
          \tkzDrawPoints(minimum);
          \node[below right] at (minimum) {$T(p,q)$};
          \node[below right,red] at (minimum|-{axis cs:0,70}) {\scriptsize $x=\pgfmathprintnumber[fixed]{\XMinimum}$};
          \node[below right,red] at (minimum-|{axis cs:-4,0}) {\scriptsize $y=\pgfmathprintnumber[fixed]{\YMinimum}$};
      }
    ]
\addplot[black,thick,mark min] {(0.73*x-1)*(x-3)*(x+2)*(x+3)};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

I used a variation of Jake's code in his answer to How can I automatically mark local extrema with pgfplots and scatter?

Gonzalo Medina
  • 505,128
4

It is easier to draw the line inside environment axis, then the coordinates are implicitly using the coordinate system axis cs. \tkzDrawPoints makes trouble this way, because I am getting a undefined \point error. Therefore, the example sets the point the same way, \tkzDrawPoints would have done it via a node with style point style:

\documentclass[symmetric,justified,a4paper,sfsidenotes]{tufte-book}
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{pgf,tikz}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,intersections,through,backgrounds,snakes}
\usetkzobj{all} %om hoeken te markeren?
\usepackage{pgfplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.5]
\begin{axis}%
    [
            anchor=center,
        grid=major,
        x=1cm, y=1cm,
        minor x tick num=1,
        xmin=-2, xmax=6,
        xlabel={\scriptsize $x$},
        axis x line=middle,
        minor y tick num=1,
        ymin=-6, ymax=6,
        ylabel={\scriptsize $f(x)$},
        tick label style={font=\tiny},
        axis y line=middle,
        no markers,
        samples=100,
        domain=-6:6,
        restrict y to domain=-20:20,
    ]
    \addplot[black, thick] (x,{2(x-2)^2-3});
    \draw[color=red, thick] (axis cs:2,-6) -- (axis cs:2,6)
      node[below right] {\scriptsize $x=2$};
    \coordinate (T) at (axis cs:2,-3);
    \node[point style] at (T) {};
    \node[below right] at (T) {$T(p,q)$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

Result

Heiko Oberdiek
  • 271,626