1

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: enter image description here

  • 1
    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
3

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

enter image description here

1

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]
\addplot[fill=lightgray,domain=-1:2,samples=100] {sqrt(abs(x))-x^2} \closedcycle;
\addplot[domain=2:3,samples=10] {sqrt(abs(x))-x^2};
\node at (axis cs: 2,-2.2) [anchor=west] {$f(x)=\sqrt{\vert x\vert}-x^2$};
\end{axis}
\end{tikzpicture}
\end{document}

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