2

I made the graph of y = \sqrt{|x|} with TikZ and the result isn't so good close to the beginning of the axes. Below you can see the difference between a photo from GeoGebra and this TeXlive.net snippet.

\documentclass[a4paper,10pt]{article}
\usepackage{pgf,tikz}

\begin{document}
\begin{tikzpicture}[scale=2]
    \draw[very thick, ->] (-2.5,0) -- (2.5,0)node[pos=1,above]{$x$};
    \draw[very thick, ->] (0,-1.2) -- (0,3)node[pos=1,left]{$y$};
     \foreach \x in {-2,...,2}\draw[shift={(\x,0)},color=black]
(0pt,0.8pt) -- (0pt,-0.8pt);
\foreach \i in {1,2}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\foreach \i in {-2,-1}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\node[below right](o) at (0,0){$0$};
     \draw[ultra thick, samples=100, draw=black, domain=-2.5:2.5] plot (\x,{sqrt(abs(\x))});
    \end{tikzpicture}
\end{document}

geogebra graph

2
  • 1
    "Below you can see the difference" -- WDYM? There's only one image.
    – Dan Mašek
    Feb 2 at 13:55
  • 1
    @DanMašek Take a look at the snippet...
    – projetmbc
    Feb 2 at 17:31

2 Answers 2

2

This interesting post explains how to use efficiently sample. This gives the following easy solution.

\documentclass[a4paper,10pt]{article}
\usepackage{pgf,tikz}

\begin{document}
\begin{tikzpicture}[scale=2]
    \draw[very thick, ->] (-2.5,0) -- (2.5,0)node[pos=1,above]{$x$};
    \draw[very thick, ->] (0,-1.2) -- (0,3)node[pos=1,left]{$y$};
     \foreach \x in {-2,...,2}\draw[shift={(\x,0)},color=black]
(0pt,0.8pt) -- (0pt,-0.8pt);
\foreach \i in {1,2}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\foreach \i in {-2,-1}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\node[below right](o) at (0,0){$0$};
     % Use 2^n + 1 for sample.
     % line join=bevel  <--  See the quark67's comment.
     \draw[ultra thick, samples=257, draw=black, domain=-2.5:2.5, line join=bevel] plot (\x,{sqrt(abs(\x))});
    \end{tikzpicture}
\end{document}
1
  • 2
    If you look carefully with a magnify glass, and with a non black drawing (eg, red), you will see the drawing is a little below the x axis. You can avoid this by adding line join=bevel in the options of the \draw command.
    – quark67
    Feb 2 at 1:49
6

I would suggest to use pgfplots for the graph. It is much easier.

\documentclass[x11names,svgnames,11pt]{article}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\centering
\begin{tikzpicture}
\begin{axis} [ width=12cm,
  xlabel = $x$, ylabel = $y$,                                                           
  ]                                                                     
                                                                        
  \addplot [red,thick,domain=-3:3,samples=500,smooth]                   
    {sqrt(abs(x))} ;                                                    

\end{axis}  
\end{tikzpicture}

\end{document}

The output:

enter image description here

3
  • 3
    Great. Replace the number of samples to an odd number like 501. So you will plot the point (0,0). With an even number of sample, you don't reach the value x=0, and in the graph, at minimum you obtain y ≈ 0.07 (if you plot 4 points evenly spaced between -3 and 3, you will plot at x=-3, x=-1, x=1 and x=3, but not at x=0).
    – quark67
    Feb 2 at 1:37
  • @mas Sometimes, we only work with GeoGebra...
    – projetmbc
    Feb 2 at 7:53
  • 2
    I understand. I just wanted to suggest that pgfplots was easier than plain tikz as your question mentions that you wanted to draw a graph.
    – mas
    Feb 2 at 8:29

You must log in to answer this question.

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