5

Hi I am trying to plot the parabola 4-x^2 over the interval -2;2 using tikz.

If I use this code I don't get the desired plot:

\begin{tikzpicture}[domain=0:2, scale = 0.75]
\draw[black, line width = 0.50mm]   plot[smooth,domain=-2:2] (\x, {4-\x^2});
\end{tikzpicture}

If I use this code, I get the desired plot.

\begin{tikzpicture}[domain=0:2, scale = 0.75]
\draw[black, line width = 0.50mm]   plot[smooth,domain=0:2] (\x, {4-\x^2});
\draw[black, line width = 0.50mm]   plot[smooth,domain=-2:0] (\x, {4+\x^2});
\end{tikzpicture}

My question: Can anyone explain me why the first one doesn't work but the second one work?

thanks

3
  • Try \x*\x, btw much easier with pgfplots then \addplot[no marks] {4-x^2}; works fine, no fuzz
    – daleif
    Aug 4, 2016 at 8:59
  • ty, \x*\x woked! i'll try to read about pgfplots! So \x^2 is not doing the correct calculation. but then if I want to plot x^6, it is kind of not fun to type \x*\x*\x*\x*\x*\x. So my new question is: Is there a way to use something like exp(\x;6) type of function?
    – kab
    Aug 4, 2016 at 9:08
  • I'll add it as an answer. You'll love pgfplots it is very very powerfull and flexible.
    – daleif
    Aug 4, 2016 at 9:12

1 Answer 1

7

It seems that tikz alone does not process \x^2 as (\x)^2 so a sign gets lost. A quick fix could be to use \x*\x which seems to work, as does (\x)^2

Even easier though is to use pgfplots

\documentclass[a4paper]{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
% bad
\begin{tikzpicture}[domain=0:2, scale = 0.75]
\draw[black, line width = 0.50mm]   plot[smooth,domain=-2:2] (\x, {4-\x^2});
\end{tikzpicture}

% works
\begin{tikzpicture}[domain=0:2, scale = 0.75]
\draw[black, line width = 0.50mm]   plot[smooth,domain=-2:2] (\x, {4-\x*\x});
\end{tikzpicture}

% works
\begin{tikzpicture}[domain=0:2, scale = 0.75]
  \draw[black, line width = 0.50mm]   plot[smooth,domain=-2:2] (\x, {4-(\x)^2});
\end{tikzpicture}

% even more fun
\begin{tikzpicture}
  \begin{axis}[
    domain=-2:2,
    ]
    \addplot[no marks] {4-x^2};
  \end{axis}
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .