Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Try this question. –  Thruston Nov 11 '13 at 18:24
4  
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. –  Benjamin McKay Nov 11 '13 at 18:26
    
And how do i do that? –  jojo Nov 11 '13 at 18:33
1  
@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. –  jfbu 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! –  jfbu Nov 11 '13 at 18:48

5 Answers 5

up vote 15 down vote accepted

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.

screenshot

% 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}
share|improve this answer
2  
x/abs(x)*abs(x)^(1/3) is a genius expression that is continuous but not differentiable. +1! –  stalking is prohibited Nov 11 '13 at 19:33
1  
@Marienplatz thanks! I'm afraid I can't take any credit for it, though :) It's a trick I got from somewhere else. –  cmhughes Nov 11 '13 at 20:01
    
Interestingly it also works for samples=11, i.e. for x=0. –  quinmars 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}

enter image description here

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}

enter image description here

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}

enter image description here

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}

enter image description here

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}

enter image description here

share|improve this answer

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}

enter image description here

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}

enter image description here

share|improve this answer

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]

share|improve this answer
    
Why do you reference to IEEE math? –  Werner Nov 11 '13 at 18:33
    
Is not it underlying pgf math? –  Boris 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}

enter image description here

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}

enter image description here

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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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