4

I'm practically new to the pgfplots package. I want to plot the quadratic equation 2x²-5x+2 on a set of axes (this is for an A-level text book). I have two questions:

  1. Why does plotting in the domain [-2.5,2,5] result in the negative part being rotated 180°? This code:

    \documentclass{minimal}
    \usepackage{tikz,pgfplots}
    \begin{document}
       \begin{center}
        \begin{tikzpicture}
        @Axes
        \draw (0.1,-0.2) node[left]{\textcolor{gray}{O}};   
        \draw[thick, color=gray,->] (-5,0) -- (5,0) node[right] {\textcolor{black}{$x$}};
        \draw[thick, color=gray, ->] (0,-1) -- (0,7) node[above] {\textcolor{black}{$f(x)$}};
    
        @Plot
        \draw [red, thick, domain=-2.5:2.5, samples=100] plot(\x, {\x^2});
        \end{tikzpicture}
       \end{center}
    \end{document}
    

    Outputs:

Output of code 1

  1. Why does changing the equation to 2x²-5x+2 in the above equation; i.e. writing plot(\x, {2\x^2-5\x+2}); not output my axes at all, but instead outputs these two pages?

Output of code 2

Any help will be greatly appreciated.

Luke Collins
  • 1,910
  • 2
    -2^2 is different from (-2)^2 – egreg Dec 07 '15 at 21:40
  • 1
  • Same problem with (\x)^2, but also there's no implicit multiplication, so you need plot (\x, {2*(\x)^2-5*(\x)+2}). And you probably want to adjust the domain.
  • – Thruston Dec 07 '15 at 21:42
  • Thanks! And how do I modify the scale of the x-axis with the y-axis in the same code? The curve looks a bit squashed. – Luke Collins Dec 07 '15 at 21:53
  • 2
    try http://tex.stackexchange.com/questions/39864/pgfplots-axis-scaling – Thruston Dec 07 '15 at 22:15
  • This seems to me pure TikZ, rather than pgfplots, am I wrong? – MattAllegro Dec 08 '15 at 16:38
  • You may be loading pgfplots, but unless you use \begin{axis} you aren't really using it. Tikz data visualization is also an option. – John Kormylo Dec 08 '15 at 16:39
  • BTW, the recommended form for a quadratic is (2x-5)x+2. It is both faster and more robust. – John Kormylo Dec 09 '15 at 03:38