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?

8
  • 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: tex.stackexchange.com/questions/19052/… – Sebastiano Sep 28 '20 at 21:49
  • 1
    @downvoter: Reason of downvote please!! – user108724 Sep 29 '20 at 9:35
  • 1
    @Sebastiano: Thanks a lot. :). – user108724 Sep 29 '20 at 9:40
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=<->,
    ]
    \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

2
  • I don't understand where that {x^(3)},{x} comes from, could you please explain? – user66094 Sep 29 '20 at 11:26
  • (x,y=x^1/3)=(y^3,y). – user108724 Sep 29 '20 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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