PGF Math Function to compute cube root
The problem with CubeRootC is the way pgf/tikz deals with ifthenelse
. The error message I get shows that when dealing with an ifthenelse(test,A,B)
, both expressions A
and B
are evaluated, then only one is kept. Since both are evaluated, you get an error message. The way my example is set, this problem is avoided. If you want to keep your code, just replace #1
by abs(#1)
in your true clause.
Here is my version of your code. The use of the exponential and logarithm functions is the best approach. The pow
function does not behave has described in the pgf/tikz manual. I shifted the plots so they don't overlap. Note also the declare function
method. I find it is easier to read; the problem is that it is local to a tikzpicture
. The code is
\documentclass{minimal}
\usepackage{tikz}
\pgfmathsetmacro{\inf}{-2}
\pgfmathsetmacro{\sup}{2}
\tikzstyle{MyPlotStyle}=[domain=\inf:\sup,samples=100,smooth]
\pgfmathdeclarefunction{CubeRootA}{1}{%
\pgfmathparse{ifthenelse(#1<0,-1,1)*exp((ln(abs(#1)))/3)}
}
\pgfmathdeclarefunction{CubeRootB}{1}{%
%\pgfmathparse{ifthenelse(#1<0,-1,1)*((abs(#1))^(1.0/3.0))}
\pgfmathparse{ifthenelse(#1<0,-1,1)*pow(abs(#1),1/3)}
}
\begin{document}
\begin{tikzpicture}
[declare function={ CubeRootC(\t)=ifthenelse(\t<0,-1,1)*exp((ln(abs(\t)))/3);}]
\draw [help lines] (-2,-2) grid [step=0.5] (2,2);
\draw[red] plot [MyPlotStyle] (\x,{CubeRootA(\x)});
\draw[blue,shift={(0,0.2)}] plot [MyPlotStyle] (\x,{CubeRootB(\x)});
\draw[green,shift={(0,-0.2)}] plot [MyPlotStyle] (\x,{CubeRootC(\x)});
\end{tikzpicture}
\end{document}
The output is
run with lualatex
\documentclass{article}
\def\cubicRoot#1{%
\directlua{%
tex.print(#1^(1/3))}}
\begin{document}
\cubicRoot{3}\par
\cubicRoot{-3}
\cubicRoot{3e-30}\par
\cubicRoot{-3e-30}
\cubicRoot{3e30}\par
\cubicRoot{-3e30}
\end{document}
and the code for the complete plot:
\documentclass{minimal}
\usepackage{tikz}
\def\cubicRoot#1{%
\directlua{%
tex.print(#1^(1/3))}}
\tikzstyle{MyPlotStyle}=[domain=-2:2,samples=100,smooth]
\begin{document}
\begin{tikzpicture}
\draw [help lines] (-2,-2) grid [step=0.5] (2,2);
\draw[red] plot [MyPlotStyle] (\x,\cubicRoot{\x});
\draw[blue,shift={(0,0.2)}] plot [MyPlotStyle] (\x,\cubicRoot{\x});
\draw[green,shift={(0,-0.2)}] plot [MyPlotStyle] (\x,\cubicRoot{\x});
\end{tikzpicture}
\end{document}
Here's a workaround using the fact that \sqrt[3]{x}
is the inverse function to x^3: A little dirty but works like a charm. Note the domain is actually the range because I've switched the x and y values.
\begin{tikzpicture}[xscale=.2,yscale=.5]
\draw[xstep=1, ystep=1, gray, very thin, dotted] (-30,-4) grid (30,4);
\draw[<->] (0,-4) -- (0,4) node[above]{$y$};
\draw[->] (-30,0) -- (30,0) node[right]{$x$};
\foreach \x in {-25,5,25} \draw(\x,1pt)--(\x,-1pt) node[below=2pt,fill=white]{\begin{tiny}$\x$\end{tiny}};
\foreach \y in {2} \draw(1pt,\y)--(-1pt,\y) node[left=2pt,fill=white]{\begin{tiny}$\y$\end{tiny}};
\draw[domain=-3.1:3.1,<->] plot ({\x^(3)},\x) node[right]{$cubert(x) = \sqrt[3]{x}$};
\draw[fill=black] (0,0) circle (2pt) node[below=10pt,right=2pt]{(0,0)};
\draw[fill=black] (1,1) circle (2pt) node[left]{(1,1)};
\draw[fill=black] (8,2) circle (2pt) node[below]{(8,2)};
\draw[fill=black] (27,3) circle (2pt) node[below]{(27,3)};
\draw[fill=black] (-1,-1) circle (2pt) node[left]{(-1,-1)};
\draw[fill=black] (-8,-2) circle (2pt) node[below]{(-8,-2)};
\draw[fill=black] (-27,-3) circle (2pt) node[below]{(-27,-3)};
\end{tikzpicture}