0

I'm trying to plot the square and cubic root of x using this code:

\begin{tikzpicture}[scale=.5, >=stealth]
\begin{axis}[
    xmin=-5,xmax=5,
    ymin=-5,ymax=5,
    axis x line=middle,
    axis y line=middle,
    axis line style=<->,
    ]
    \addplot[no marks,blue] expression[samples=100]{sqrt{x}} 
                node[pos=0.65,anchor=south west]{$y=\sqrt{x}$}; 

    \addplot[no marks,red] expression[domain=-pi:pi,samples=100]{sqrt[3]{x}} 
                node[pos=0.65,anchor=south west]{$y=\sqrt[3]{x}$}; 
\end{axis}

\end{tikzpicture}

but I get the following errors: enter image description here

I've tried writing \sqrt instead of sqrt but the errors are the same. How can I solve this?


Ok so, trying, as suggested by a comment, to try different formulas I got it working with x^(1/2) and x^(1/3)

The problem now is that I get the wrong graph for the cube root.

The code now is:

             \begin{tikzpicture}[scale=.5, >=stealth]
\begin{axis}[
    xmin=-5,xmax=5,
    ymin=-5,ymax=5,
    axis x line=middle,
    axis y line=middle,
    axis line style=<->,
    ]
    \addplot[no marks,blue] expression[domain=0:5,samples=100]{x^(1/2)} 
                node[pos=0.65,anchor=south west]{$y=\sqrt{x}$}; 

    \addplot[no marks,red] expression[domain=-5:5,samples=100]{x^(1/3)} 
                node[pos=0.65,anchor=south west]{$y=\sqrt[3]{x}$}; 
\end{axis}

\end{tikzpicture}

But for the cube root I'm missing the negative branch:enter image description here

How can I solve this?

user66094
  • 379
  • 3
    Have you tried with sqrt(x) with the rounded bracket. This is the correct syntax. – Sebastiano Sep 28 '20 at 21:35
  • 1
    Yes. It didn't work neither but it worked when I used x^(1/2) and x^(1/3). Though for the latter it gives the wrong graph. – user66094 Sep 28 '20 at 21:38
  • 1
    Try with sqrt(x) and exp(ln(x)/3) for the square and cubic root. Please, can you put your complete MWE? Just for me there are some mistakes to compile it. See after also this link: https://tex.stackexchange.com/questions/19052/pgf-math-function-to-compute-cube-root – Sebastiano Sep 28 '20 at 21:49
  • Using that randabout formula didn't work but once again you made me think of another solution: I simply graphed the function in two pieces: x^(1/3) for x>0 and -(-x)^(1/3) for x<=0 – user66094 Sep 29 '20 at 05:25
  • 1
    @downvoter: Reason of downvote please!! –  Sep 29 '20 at 09:35
  • @C.F.G Don't worry...I have increased to zero. – Sebastiano Sep 29 '20 at 09:39
  • 1
    @Sebastiano: Thanks a lot. :). –  Sep 29 '20 at 09:40
  • 1
    Please make sure to always post full minimal examples with document class and minimal preamble. That makes it easier to test the code under the same conditions as you are using the code. – daleif Sep 29 '20 at 09:56

1 Answers1

1

I don't really know why TikZ can't plot correctly but here is a trick: graph of x^(1/3) is same as (x^3,x) (if I am not mistaken):

\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz,pgfplots}
\begin{document}
    \begin{tikzpicture}[scale=1, >=stealth]
\begin{axis}[
xmin=-5,xmax=5,
ymin=-5,ymax=5,
axis x line=middle,
axis y line=middle,
axis line style=&lt;-&gt;,
]
\addplot[red,domain=-5:5,samples=100]({x^(3)},{x})
node[pos=0.65,anchor=south]{$y=\sqrt[3]{x}$};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here