# How to plot x^(1/3)?

I tried to export my Geogebra graphs into tikz-code. In General it works fine, but plot x^1/3 doesn't work:

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\definecolor{ccqqqq}{rgb}{0.8,0,0}
\definecolor{qqttcc}{rgb}{0,0.2,0.8}
\definecolor{cqcqcq}{rgb}{0.75,0.75,0.75}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw [color=cqcqcq,dash pattern=on 1pt off 1pt, xstep=1.0cm,ystep=1.0cm] (-4,-4) grid (4,4);
\draw[->,color=black] (-4,0) -- (4,0);
\foreach \x in {-4,-3,-2,-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0,-4) -- (0,4);
\foreach \y in {-4,-3,-2,-1,1,2,3}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-4,-4) rectangle (4,4);
\draw[color=qqttcc, smooth,samples=100,domain=-4.0:4.0] plot(\x,{(\x)*(\x)*(\x)});
\draw[color=ccqqqq, smooth,samples=100,domain=-4.0:4.0] plot(\x,{((\x))^(1/(3))});
\draw [color=qqttcc](2.06,4.18) node[anchor=north west] {$f(x)=x^3$};
\draw [color=ccqqqq](2.06,1.18) node[anchor=north west] {$f(x)=\sqrt[3]{x}$};
\begin{scriptsize}
\draw[color=qqttcc] (-1.74,-7.16) node {$f$};
\end{scriptsize}
\end{tikzpicture}
\end{document}


Error line 21: !Missing number, treated as zero.

So plot has problems with the negative x I suppose, but I don't know. Any help would be great! I googled it for nearly 1 hour - but no real answers.

As suggestet it rotated and mirrored the graph - works great!:

\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\definecolor{ccqqqq}{rgb}{0.8,0,0}
\definecolor{qqttcc}{rgb}{0,0.2,0.8}
\definecolor{cqcqcq}{rgb}{0.75,0.75,0.75}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw [color=cqcqcq,dash pattern=on 1pt off 1pt, xstep=1.0cm,ystep=1.0cm] (-4,-4) grid (4,4);
\draw[->,color=black] (-4,0) -- (4,0);
\foreach \x in {-4,-3,-2,-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize \x};
\draw[->,color=black] (0,-4) -- (0,4);
\foreach \y in {-4,-3,-2,-1,1,2,3}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize \y};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize 0};
\clip(-4,-4) rectangle (4,4);
\draw[color=qqttcc, smooth,samples=100,domain=-4.0:4.0] plot(\x,{(\x)*(\x)*(\x)});
\begin{scope}[yscale=-1,xscale=1]
\draw[rotate=90, color=ccqqqq, smooth,samples=100,domain=-4.0:4.0] plot(\x,{(\x)*(\x)*(\x)});
\end{scope}
\draw [color=qqttcc](2.06,4.00) node[anchor=north west] {$f(x)=x^3$};
\draw [color=ccqqqq](2.06,1.00) node[anchor=north west] {$f(x)=\sqrt[3]{x}$};
\begin{scriptsize}
\draw[color=qqttcc] (-1.74,-7.16) node {f};
\end{scriptsize}
\end{tikzpicture}
\end{document}

• Try this question. Nov 11 '13 at 18:24
• The elegant way would be to plot $y=x^3$ but reflected across the line $y=x$. That way you never need to compute a cube root, so computations are faster and, more importantly, more accurate. Nov 11 '13 at 18:26
• And how do i do that?
– jojo
Nov 11 '13 at 18:33
• @BenjaminMcKay I tried \draw[color=ccqqqq, smooth,samples=50,domain=0.0001:4.0] plot(\x, {(\x)^(1/3)}); with the same code as jojo but the shape of the curve is completely wrong for values less than 1.0.
– user4686
Nov 11 '13 at 18:39
• ah ok, I understand that with \usepackage{fp} \usetikzlibrary {fixedpointarithmetic} and the key fixed point arithmetic added to the picture, it now works. The failure of naked pgfmath on this one is spectacular!
– user4686
Nov 11 '13 at 18:48

## 6 Answers

I'm sure we've answered this before, but here's a solution using pgfplots.

When plotting cube root functions it is useful to know that many programs (including the wonderful pgfplots package) use logarithms to plot them. As such, you have to be careful with the domain.

In the code below, I have plotted the function

x/|x|*(|x|)^(1/3)


which ensures that the function is plotted for the entire domain.

% arara: pdflatex
\documentclass{standalone}
\usepackage{pgfplots}

% set the arrows as stealth fighters
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-10,xmax=10,
ymin=-10,ymax=10,
axis lines=center,
axis line style=<->]
\addplot[<->] expression[domain=-10:10,samples=100]{x/abs(x)*abs(x)^(1/3)};
\end{axis}
\end{tikzpicture}
\end{document}

• x/abs(x)*abs(x)^(1/3) is a genius expression that is continuous but not differentiable. +1! Nov 11 '13 at 19:33
• @Marienplatz thanks! I'm afraid I can't take any credit for it, though :) It's a trick I got from somewhere else. Nov 11 '13 at 20:01
• Interestingly it also works for samples=11, i.e. for x=0. Nov 11 '13 at 20:46

With PSTricks.

## Option 1 (with Postfix Notation)

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-3.5,-2.5)(4,3)
\psaxes{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\pstVerb{/power 1 3 div def}
\psplot[plotpoints=1000,linecolor=blue]{-3}{3}{x dup 0 lt {neg power exp neg} {power exp} ifelse}
\end{pspicture}
\end{document}


## Option 2 (with Infix Notation)

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-3.5,-2.5)(4,3)
\psaxes{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\psplot[plotpoints=1000,linecolor=blue,algebraic]{-3}{3}{IfTE(x<0,-(-x)^(1/3),x^(1/3))}
\end{pspicture}
\end{document}


## Option 3 (with Rotation)

This is the last resort only for children. The right leaf is rotated 180 degrees about the origin to get the left leaf.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{plotpoints=1000}

\begin{document}
\multido{\i=0+10}{19}{%
\begin{pspicture}(-3.5,-2.5)(4,3)
\psaxes{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\def\right{\psplot[linecolor=blue,algebraic]{0}{3}{x^(1/3)}}%
\rput{\i}{\right}\right
%\def\right{\psplot[linecolor=red]{0}{3}{x 1 3 div exp}}%
%\rput{180}{\right}\right
\end{pspicture}}
\end{document}


## Option 4 (with Parametric Plot)

Please see Herbert's answer.

## Option 5 (with Implicit Plot)

With an extra bug.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-func}

\begin{document}
\begin{pspicture}(-3.5,-2.5)(4,3)
\psaxes{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\psplotImp[linecolor=red,stepFactor=0.2,algebraic](-3,-1.5)(3,1.5){y^3-x}
\end{pspicture}
\end{document}


The bug can be hidden by clipping the unwanted curves.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-func}

\begin{document}
\begin{pspicture}(-3.5,-2.5)(4,3)
\psaxes{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\psclip{\psframe[linestyle=none,dimen=monkey](!-3 3 1 3 div exp neg)(!3 3 1 3 div exp)}
\psplotImp[linecolor=red,stepFactor=0.1,algebraic](-4,-3)(5,4){y^3-x}
\endpsclip
\end{pspicture}
\end{document}


## Option 6 (with swapaxes)

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-3.5,-2.5)(4,3)
\psaxes{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\psplot[linecolor=red,algebraic,plotpoints=1000,swapaxes]{3 1 3 div exp neg}{3 1 3 div exp}{x^3}
\end{pspicture}
\end{document}


use the parametric form: x=t³ and y=t:

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-3.5,-2.5)(4,3)
\psaxes[labelFontSize=\scriptstyle]{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\psparametricplot[linecolor=blue,algebraic]{-1.5}{1.5}{t^3|t}
\end{pspicture}
\end{document}


or the implicit form y³-x=0:

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-func}

\begin{document}
\begin{pspicture*}(-3.5,-2.5)(4,3)
\psaxes[labelFontSize=\scriptstyle,ticksize=0 4pt]{->}(0,0)(-3.5,-2.5)(3.5,2.5)[$x$,0][$y$,90]
\psplotImp[linecolor=red,linewidth=2pt,algebraic](-4,-3)(4,3){y^3-x}
\end{pspicture*}
\end{document}


IEEE math usually gives NaN (not a number) for x^a when x ≤ 0 and a is not integer.

You can draw x^{1/3} for x in [0.00001:4] and -(-x)^{1/3} for x in [-4:-0.00001]

• Why do you reference to IEEE math? Nov 11 '13 at 18:33
• Is not it underlying pgf math? Nov 11 '13 at 18:34

Another approach would be to use gnuplot to compute the 1/3 floating point expressions with -shell-escape enabled and gnuplot installed.

The idea is to evaluate 1/3 first similar to making atleast one numerator or denominator floating point value like this 1./3 or 1.0/3. Note: sgn(x) is Sign function (-1 if x < 0; 0 if x = 0; 1 if x > 0). One might need more samples of points to get a refined plot. Here are some examples with packages that use gnuplot.

With tkz-fct

\documentclass[preview=true,12pt]{standalone}
\usepackage{tkz-fct}
\begin{document}
\begin{tikzpicture}[scale=2]
\tkzInit[xmin=-2,xmax=2,ymin=-2,ymax=2]
\tkzGrid
\tkzAxeXY
\tkzFct[color=red]{sgn(x)*(abs(x)**(1./3))}
\end{tikzpicture}
\end{document}


With gnuplottex

\documentclass[preview=true,12pt]{standalone}
\usepackage{gnuplottex}
\begin{document}
\begin{gnuplot}[terminal=epslatex,terminaloptions=color]
set grid
set samples 1000
set xlabel '$x$'
set ylabel '$x^{\frac{1}{3}}$'
plot [-2:2] [-2:2] sgn(x)*(abs(x)**(1./3)) title '$x^{\frac{1}{3}}$' linetype 1 linewidth 3
\end{gnuplot}
\end{document}


With pgfplots and even pure tikz can be used.

\documentclass[preview=true,border=2pt,12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$x^{\frac{1}{3}}$,grid=major,enlargelimits=false]
\addplot [domain=-2:2,samples=1000,red,no markers] gnuplot[id=poly]{sgn(x)*(abs(x)**(1./3)) };
\end{axis}
\end{tikzpicture}
\end{document}


\begin{tikzpicture}[scale=0.5]
\draw[dashed,gray!30](-3,-3)grid(3,3); %this creates the grid you see;
%dashed, gives the dashed lines
%gray!30, gives a gray color be dim by factor of 30.
\draw[<->](-3,0)--(3,0)node[below right]{$x$}; %this is the x-axis
\draw[<->](0,-3)--(0,3)node[above right]{$y$}; %this is the y-axis
\foreach \x in {-2,-1,1,2} %this starts a while loop with "\x" touching -2,-1,1,2 then ends.
\draw(\x,2pt)--(\x,-2pt)node[below]{\footnotesize{$\x$}}; %this is what I want the foreach
%loop to do until it reaches the semi-colon.
%this is what gives the tick marks and numbers on the axis
\foreach \x in {-2,-1,1,2} %same as before but for the y-axis
\draw(2pt,\x)--(-2pt,\x)node[left]{\footnotesize{$\x$}};
\draw[<->,domain=-1.442249:1.442249,samples=100]plot({\x*\x*\x},\x); %where x^3=2 when x=1.442249'ish
\end{tikzpicture}