# Why does tikz not plot this even function correctly? [duplicate]

Can someone provide an explanation and a fix for the fact that my plot is not what is expected? The function is symmetric with respect to the y-axis. The plot correctly shows the function for positive values of x, but not so for negative values. Here is my code:

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usepackage{fouriernc}
\begin{document}
\begin{tikzpicture}[>=latex,x=1.3cm]
\fill[fill=lightgray] (0,0) -- plot[domain=0:1,samples=100] (\x,{sqrt(abs(\x))-\x^2});
\fill[fill=lightgray] plot[domain=1:2,samples=100] (\x,{sqrt(abs(\x))-\x^2}) -- (2,0);
\draw[thick,domain=-1:2.2,samples=100] plot (\x,{sqrt(abs(\x))-\x^2}) node[right] {\footnotesize $f(x)=\sqrt{|x|}-x^2$};
\foreach \x in {-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\foreach \y in {-3,-2,-1,1}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[->,thick] (-2,0) -- (4,0) node[above left]{\footnotesize $x$};
\draw[->,thick] (0,-4) -- (0,2) node[below right]{\footnotesize $f(x)$};
\end{tikzpicture}
\end{document}


Here is what I get with this code:

• Try plot (\x,{sqrt(abs(\x))-(\x)^2}) (I added parentheses). – jub0bs Dec 8 '13 at 20:00
• Please check my answer below where I create the same plot with pgfplots (less code, easier to understand and no (\x)). – remus Dec 9 '13 at 8:36
• I like your solution which is why I up-voted it. However, I didn't check it because I wanted to know why my original didn't work and how I could fix it without using pgfplots. – DJJerome Dec 10 '13 at 14:33

# Remarks

In the expression \x^2 you need to wrap \x in parantheses, don't ask me why.

# Implementation

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[>=latex,x=1.3cm]
\fill[fill=lightgray] (0,0) -- plot[domain=0:1,samples=100] (\x,{sqrt(abs(\x))-(\x)^2});
\fill[fill=lightgray] plot[domain=1:2,samples=100] (\x,{sqrt(abs(\x))-(\x)^2}) -- (2,0);
\draw[thick,domain=-1:2.2,samples=100] plot (\x,{sqrt(abs(\x))-(\x)^2}) node[right] {\footnotesize $f(x)=\sqrt{|x|}-x^2$};
\foreach \x in {-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\foreach \y in {-3,-2,-1,1}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[->,thick] (-2,0) -- (4,0) node[above left]{\footnotesize $x$};
\draw[->,thick] (0,-4) -- (0,2) node[below right]{\footnotesize $f(x)$};
\end{tikzpicture}
\end{document}


# Output

• Actually, that makes sense. (-x)^2 is not equal to -x^2. Good old order of operations! – DJJerome Dec 8 '13 at 20:06
• @DJJerome But why is \x not equal to (\x) in PGF? – Henri Menke Dec 8 '13 at 20:06
• @HenriMenke: see tex.stackexchange.com/questions/5400/… – Jake Dec 8 '13 at 20:09

Maybe an easier way to plot your function that removes the ambiguity on \x^2:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{lmodern}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis on top=true,
xlabel={$x$},
ylabel={$f(x)$},
axis x line=middle,
axis y line=middle,
xmin=-2,
xmax=4.9,
ymax=1.4,
ymin=-4]
\node at (axis cs: 2,-2.2) [anchor=west] {$f(x)=\sqrt{\vert x\vert}-x^2$};