17

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}
jojo
  • 173

8 Answers8

23

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}
cmhughes
  • 100,947
18

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 this 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] \psplotImplinecolor=red,stepFactor=0.2,algebraic(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)} \psplotImplinecolor=red,stepFactor=0.1,algebraic(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

AKP2002
  • 105
14

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

3

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]

azetina
  • 28,884
Boris
  • 38,129
2

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

1
\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}
0
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm,scale=.75]
    \draw [line width=0.3pt,pink](-3,-2) grid (3,2);
    \draw[>=latex,->,color=black,] (-3,0) -- (3,0);
    \foreach \x in {-2,2}
    \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
    \draw[>=latex,->,color=black] (0,-2) -- (0,2);
    \foreach \y in {}
    \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
    %\draw[color=black] (0pt,-1pt) node[below left] {\footnotesize $0$};
    \node at (3,0) [right] {\small $x$};
    \node at (0,2) [above ] {\small $y$};
    \draw[line width=1pt,red,domain=-1.442249:1.442249,samples=200]plot({\x*\x*\x},\x); 
\end{tikzpicture}
kawsar
  • 63
0

The arc of the curve of a polynomial of order 3 is a cubic Bézier spline.

For example, the curve of x^(1/3) on [-8,8] is the cubic Bézier spline corresponding to the points (-8,-2), (8,-2/3), (-8,2/3) and (8,2).

Here is that cubic spline drawn with Tikz.

\documentclass{article}
\usepackage{tikz}

\usepackage{geometry}

\begin{document}

\begin{center} \begin{tikzpicture} [scale = 0.7] \draw [->] (-8.5,0) -- (8.5,0) ; \draw [->] (0,-2.5) -- (0,2.5) ; \draw [red] (-8,-2) .. controls (8,-0.666) and (-8,0.666) .. (8,2) ; \end{tikzpicture} \end{center}

\end{document}

Output of the code with tikz

Here is the code with pstricks.

\documentclass{article}
\usepackage{pstricks}

\usepackage{geometry}

\begin{document}

\begin{center} \psset{unit=7mm} \begin{pspicture}(-9,-3)(9,3) \psline{->}(-8.5,0)(8.5,0) \psline{->}(0,-2.5)(0,2.5) \psbezierlinecolor=red(8,-0.666)(-8,0.666)(8,2) \end{pspicture} \end{center}

\end{document}

Output of the code with pstricks

F. Pantigny
  • 40,250